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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:51:38+00:00 2026-05-21T18:51:38+00:00

Suppose I have an R dataframe like this: Subject Session Property.A Property.B Property.C 1

  • 0

Suppose I have an R dataframe like this:

  Subject Session  Property.A Property.B Property.C
1     100       1 -1.22527548 -0.9193751 -1.7501693
2     100      10  2.30627980  1.8940830 -0.8443976
3     100       2  2.33243332 -0.5860868 -4.2074489
4     100       3  0.38130810 -0.7336206  4.8016230
5     100       4  1.44685875  0.5066249  2.0138624
6     100       5  0.08907721 -0.3715202  1.4983700

I have heard this style of data frame referred to as “short form” or “wide form”. Now suppose I want to make it look like this, which I have heard called “long form”:

  Subject Session  Property    Value
1     100       1         A   -1.2252754
2     100       1         B   -0.9193751
3     100       1         C   -1.7501693
4     100       2         A    2.3324333
5     100       2         B   -0.5860868
6     100       2         C   -4.2074489

That is, I have N columns that I want to reduce to just two “name/value” columns, with any other columns in the dataframe extended with repeated values as necessary.

Obviously I could perform this conversion with a bunch of for loops, but that seems really ugly, and it would be a pain to maintain if/when I add more property columns.

Is there a way to do this in R with just a few lines of code? Some magic combination of functions I haven’t discovered yet?

  • 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-05-21T18:51:38+00:00Added an answer on May 21, 2026 at 6:51 pm

    Use the melt function in package reshape2:

    library(reshape2)
    dat.m <- melt(dat, id.vars = c("Subject", "Session"))
    

    If you need to clean up the column names and/or values for the variable column:

    #change "variable" to "Property"
    names(dat.m)[3] <- "Property"
    #Drop "Property." from the column values
    dat.m$Property <- gsub("Property\\.", "", dat.m$Property)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
Suppose I have an R dataframe that looks like this, where end.group signifies the
Suppose I have a dataframe 'H', like so C1 C2 a 1 b 1
Suppose I have a data frame like this: df <- data.frame(id = rep(c(1001:1004), each
Suppose I have a DataFrame which has a subindex structure like the following, with
Suppose, you have a data.frame like this: x <- data.frame(v1=1:20,v2=1:20,v3=1:20,v4=letters[1:20]) How would you select
Suppose we have the name written in any none-latin letters - languages, like Arabic,
Suppose I have a DataFrame with 100k rows and a column name . I
Suppose we have a dataframe or matrix with one column specifying an integer value
Suppose I have a date.frame like: df <- data.frame(a=1:5, b=sample(1:5, 5, replace=TRUE), c=5:1) df

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.