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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:26:15+00:00 2026-06-09T21:26:15+00:00

Apologies for the seemingly simple question, but I can’t seem to find a solution

  • 0

Apologies for the seemingly simple question, but I can’t seem to find a solution to the following re-arrangement problem.

I’m used to using read.csv to read in files with a header row, but I have an excel spreadsheet with two ‘header’ rows – cell identifier (a, b, c … g) and three sets of measurements (x, y and z; 1000s each) for each cell:

a           b       
x    y  z   x   y   z
10   1  5   22  1   6
12   2  6   21  3   5
12   2  7   11  3   7
13   1  4   33  2   8
12   2  5   44  1   9

csv file below:

a,,,b,,
x,y,z,x,y,z
10,1,5,22,1,6
12,2,6,21,3,5
12,2,7,11,3,7
13,1,4,33,2,8
12,2,5,44,1,9

How can I get to a data.frame in R as shown below?

cell x  y   z
a    10 1   5
a    12 2   6
a    12 2   7
a    13 1   4
a    12 2   5
b    22 1   6
b    21 3   5
b    11 3   7
b    33 2   8
b    44 1   9
  • 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-09T21:26:17+00:00Added an answer on June 9, 2026 at 9:26 pm

    Use base R reshape():

    temp = read.delim(text="a,,,b,,
    x,y,z,x,y,z
    10,1,5,22,1,6
    12,2,6,21,3,5
    12,2,7,11,3,7
    13,1,4,33,2,8
    12,2,5,44,1,9", header=TRUE, skip=1, sep=",")
    names(temp)[1:3] = paste0(names(temp[1:3]), ".0")
    OUT = reshape(temp, direction="long", ids=rownames(temp), varying=1:ncol(temp))
    OUT
    #     time  x y z id
    # 1.0    0 10 1 5  1
    # 2.0    0 12 2 6  2
    # 3.0    0 12 2 7  3
    # 4.0    0 13 1 4  4
    # 5.0    0 12 2 5  5
    # 1.1    1 22 1 6  1
    # 2.1    1 21 3 5  2
    # 3.1    1 11 3 7  3
    # 4.1    1 33 2 8  4
    # 5.1    1 44 1 9  5
    

    Basically, you should just skip the first row, where there are the letters a-g every third column. Since the sub-column names are all the same, R will automatically append a grouping number after all of the columns after the third column; so we need to add a grouping number to the first three columns.

    You can either then create an “id” variable, or, as I’ve done here, just use the row names for the IDs.

    You can change the “time” variable to your “cell” variable as follows:

    # Change the following to the number of levels you actually have
    OUT$cell = factor(OUT$time, labels=letters[1:2])
    

    Then, drop the “time” column:

    OUT$time = NULL
    

    Update

    To answer a question in the comments below, if the first label was something other than a letter, this should still pose no problem. The sequence I would take would be as follows:

    temp = read.csv("path/to/file.csv", skip=1, stringsAsFactors = FALSE)
    GROUPS = read.csv("path/to/file.csv", header=FALSE, 
                      nrows=1, stringsAsFactors = FALSE)
    GROUPS = GROUPS[!is.na(GROUPS)]
    names(temp)[1:3] = paste0(names(temp[1:3]), ".0")
    OUT = reshape(temp, direction="long", ids=rownames(temp), varying=1:ncol(temp))
    OUT$cell = factor(temp$time, labels=GROUPS)
    OUT$time = NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies for this seemingly minor question, but I can't seem to find the answer
Apologies for this question but I can't find proper answer on the web. I'm
Apologies for a simple question, but I'm very new to Perl! I have an
Apologies for simple the question - I haven't been able to find a suitable
Apologies for probably simple question, I've read the docs and still can't get this
Apologies if this is a too basic question but I couldn't find any satisfactory
Apologies for the vagueness of the question here but can someone point me to
I apologize for this seemingly relatively simple task that I can't figure out. I
I recognise this is a popular question but I couldn't find anything that answered
Apologies or asking what is probably a very straightforward question, but I'm new to

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.