Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9213115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:40:28+00:00 2026-06-18T01:40:28+00:00

Very simple question. I am using an excel sheet that has two rows for

  • 0

Very simple question. I am using an excel sheet that has two rows for the column headings; how can I convert these two row headings into one? Further, these headings don’t start at the top of the sheet.

Thus, I have DF1

Temp Press  Reagent  Yield A  Conversion etc
degC bar    /g       %        %             
1    2      3        4        5          
6    7      8        9        10

and I want,

Temp degC Press bar  Reagent /g  Yield A % Conversion etc
1         2          3           4         5          
6         7          8           9        10

Using colnames(DF1) returns the upper names, but getting the second line to merge with the upper one keeps eluding me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:40:29+00:00Added an answer on June 18, 2026 at 1:40 am

    Using your data, modified to quote text fields that contain the separator (get whatever tool you used to generate the file to quote text fields for you!)

    txt <- "Temp Press  Reagent  'Yield A'  'Conversion etc'
    degC bar    /g       %        %             
    1    2      3        4        5          
    6    7      8        9        10
    "
    

    this snippet of code below reads the file in two steps

    1. First we read the data, so skip = 2 means skip the first 2 lines
    2. Next we read the data again but only the first two line, this output is then further processed by sapply() where we paste(x, collapse = " ") the strings in the columns of the labs data frame. These are assigned to the names of dat

    Here is the code:

    dat <- read.table(text = txt, skip = 2)
    labs <- read.table(text = txt, nrows = 2, stringsAsFactors = FALSE)
    names(dat) <- sapply(labs, paste, collapse = " ")
    
    dat
    names(dat)
    

    The code, when runs produces:

    > dat <- read.table(text = txt, skip = 2)
    > labs <- read.table(text = txt, nrows = 2, stringsAsFactors = FALSE)
    > names(dat) <- sapply(labs, paste, collapse = " ")
    > 
    > dat
      Temp degC Press bar Reagent /g Yield A % Conversion etc %
    1         1         2          3         4                5
    2         6         7          8         9               10
    > names(dat)
    [1] "Temp degC"        "Press bar"        "Reagent /g"      
    [4] "Yield A %"        "Conversion etc %"
    

    In your case, you’ll want to modify the read.table() calls to point at the file on your file system, so use file = "foo.txt" in place of text = txt in the code chunk, where "foo.txt" is the name of your file.

    Also, if these headings don’t start at the top of the file, then increase skip to 2+n where n is the number of lines before the two header rows. You’ll also need to add skip = n to the second read.table() call which generates labs, where n is again the number of lines before the header lines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very simple question, hopefully. So, in Python you can split up strings using indices
I have a very simple question that i can't seem to find the answer,
I think that's a very simple question, but I can't deal with it: I
I have a feeling that this question is very simple, but I just can't
Very simple question, but I want to start using a consistent naming convention for
Very simple question: Specifically in Python (since Python actually has strongly recommended style guidelines
A very simple question I can't seem to find a definitive answer for. I
A very simple question: I am writing and running my R scripts using a
This should be a very simple question. I have a richfaces tree that is
This isn't a very simple question, but hopefully someone has run across it. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.