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 8437065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:20:04+00:00 2026-06-10T07:20:04+00:00

Possible Duplicate: Read csv with two headers into a data.frame I am new to

  • 0

Possible Duplicate:
Read csv with two headers into a data.frame

I am new to R and working to use R to analyse some data. The data happens to be in Excel format and right now I’m struggling to find a way to convert it into a format that is R-friendly.

The issue is that the column headers have merged cells, so in effect the headers have two rows. I’d like to convert it into a normal set of 1-D vectors, add an extra column and a row. Let me explain with an example:

Currently the excel format looks thus:

  |  H  |  J  | 
Y |M |F |M |F | 
== == == == == 
Y1|V1|V2|V3|V4|

H,J are merged column headers and each of them span columns M and F.

The = indicate that the rows above are header rows

Given that H,J both are elements under, say R, I would like to convert this into a columnar format with a normal header and two rows, like this

Y |R |M |F |
== == == ==
Y1|H |V1|V2|
Y1|J |V3|V4|

Does anyone have an idea how to do this?

  • 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-10T07:20:05+00:00Added an answer on June 10, 2026 at 7:20 am

    First, some assumptions:

    • The merged headings are on the first line of the CSV
    • The merged headings start in the second column of the CSV
    • The variable names in the second line of the CSV repeat (except for the variable in the first column)

    Second, your data.

    temp = c(",\"H\",,\"J\",", 
             "\"Y\",\"M\",\"F\",\"M\",\"F\"", 
             "\"Y1\",\"V1\",\"V2\",\"V3\",\"V4\"")
    

    Third, a slightly modified version of this answer.

    # check.names is set to FALSE to allow variable names to be repeated
    ONE = read.csv(textConnection(temp), skip=1, check.names=FALSE,
                   stringsAsFactors=FALSE)
    GROUPS = read.csv(textConnection(temp), header=FALSE, 
                      nrows=1, stringsAsFactors=FALSE)
    GROUPS = GROUPS[!is.na(GROUPS)]
    
    # This can be shortened, but I've written it this way to show how
    #   it can be generalized. For instance, if 3 columns were repeated
    #   instead of 2, the rep statement could be changed to reflect that
    names(ONE)[-1] = paste0(names(ONE)[-1], ".",
                            rep(GROUPS, each=(length(names(ONE)[-1])/2)))
    

    Fourth, the actual reshaping of the data.

    TWO = reshape(ONE, direction="long", ids=1, varying=2:ncol(ONE))
    # And, here's the output.
    TWO
    #      Y time  M  F id
    # 1.H Y1    H V1 V2  1
    # 1.J Y1    J V3 V4  1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: how to extract data from csv file in php i'm new on
Possible Duplicate: Read data from a file individually and multiply the two columns in
Possible Duplicate: C# - Deleting a file permanently I read some articles how data
Possible Duplicate: Read Post Data submitted to ASP.Net Form I have a google checkout
Possible Duplicate: Python: How to read huge text file into memory To process a
Possible Duplicate: Read local compressed XML files(gzip) in Android application Hi I am working
Possible Duplicate: Use LINQ to read all nodes from XML I am trying to
Possible Duplicate: Some issues trying to read a file with cbc.read.table function in R
Possible Duplicate: New Integer vs valueOf I read at few place that Integer.valueOf is
Possible Duplicate: casting unused return values to void I read some source code, and

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.