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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:39:28+00:00 2026-06-11T10:39:28+00:00

I’m trying to change a data frame column ( var3 , in the example

  • 0

I’m trying to change a data frame column (var3, in the example below) that has multiple values for factor levels of another variable (names, in the example below). I’d like var3 to be split into separate columns, one for each value, so that the factor levels in names do not repeat. My other variables (var1, var2) repeat where necessary to provide space for var3.

This is the kind of data I have:

df1 <- structure(list(name = structure(c(2L, 4L, 4L, 4L, 3L, 5L, 5L, 
1L), .Label = c("fifth", "first", "fourth", "second", "third"
), class = "factor"), var1 = c(90L, 84L, 84L, 84L, 18L, 22L, 
22L, 36L), var2 = c(301L, 336L, 336L, 336L, 412L, 296L, 296L, 
357L), var3 = c(-0.582075925, -1.108889624, -1.014962009, -0.162309524, 
-0.282309524, 0.563055819, -0.232075925, -0.773353424)), .Names = c("name", 
"var1", "var2", "var3"), class = "data.frame", row.names = c(NA, -8L))

This is what i’d like:

df2 <- structure(list(name = structure(c(2L, 4L, 3L, 5L, 1L), .Label = c("fifth", 
"first", "fourth", "second", "third"), class = "factor"), var1 = c(90L, 
84L, 18L, 22L, 36L), var2 = c(301L, 336L, 412L, 296L, 357L), 
var3 = c(-0.582075925, -1.108889624, -0.282309524, 0.563055819, 
-0.773353424), var3.2 = c(NA, -1.014962009, NA, -0.232075925, 
NA), var3.3 = c(NA, -0.162309524, NA, NA, NA)), .Names = c("name", "var1", 
"var2", "var3", "var3.2", "var3.3"), class = "data.frame", row.names = c(NA, -5L))

I’ve looked at reshape and ddply, but can’t get them to give me this output.

  • 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-11T10:39:29+00:00Added an answer on June 11, 2026 at 10:39 am

    Here’s a base solution:

    > df1$seqnam <- ave(as.character(df1$name), df1$name, FUN=seq)  # creates a "time" index 
    > reshape(df1, direction="wide", timevar="seqnam",  idvar=c("name", "var1", "var2") )
        name var1 var2     var3.1     var3.2     var3.3
    1  first   90  301 -0.5820759         NA         NA
    2 second   84  336 -1.1088896 -1.0149620 -0.1623095
    5 fourth   18  412 -0.2823095         NA         NA
    6  third   22  296  0.5630558 -0.2320759         NA
    8  fifth   36  357 -0.7733534         NA         NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.