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

  • Home
  • SEARCH
  • 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 8032239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:19:53+00:00 2026-06-05T01:19:53+00:00

I am looking for an elegant way to basically split a data frame by

  • 0

I am looking for an “elegant” way to basically split a data frame by the levels of one column variable, then create a new output data frame reshaped to now drop the factor variable and add new columns for the levels of the factor variable. I can do this with functions such as the split() method, but this seems to be the messy way to me. I have been trying to do this using the melt() and cast() functions in the plyr package, but haven’t been successful in getting the exact output I need.

Here is what my data looks like:

> jumbo.df = read.csv(...)
> head(jumbo.df)
         PricingDate  Name     Rate 
    186  2012-03-05   Type A   2.875  
    187  2012-03-05   Type B   3.250  
    188  2012-03-05   Type C   3.750  
    189  2012-03-05   Type D   3.750  
    190  2012-03-05   Type E   4.500  
    191  2012-03-06   Type A   2.875

What I would like to do is split by the variable name, remove Name and Rate, then output columns for Type A, Type B, Type C, Type D, and Type E with the corresponding Rate series with Date as ID:

> head(output.df)
         PricingDate  Type A   Type B    Type C    Type D    Type E 
         2012-03-05    2.875    3.250     3.750     3.750     4.500  
         2012-03-06    2.875    ...

Thanks!

  • 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-05T01:19:54+00:00Added an answer on June 5, 2026 at 1:19 am

    Not sure if I get you right, but could it be that you just want to reshape your data into the wide format? If so, you have to use the melt and cast functions of the reshape (!) package. reshape2 is basically the same. Since your data is already in the molten format, i.e. the long format, a one-liner does what you want:

    df <- read.table(textConnection("PricingDate  Name     Rate 
                                     2012-03-05   TypeA   2.875  
                                     2012-03-05   TypeB   3.250  
                                     2012-03-05   TypeC   3.750  
                                     2012-03-05   TypeD   3.750  
                                     2012-03-05   TypeE   4.500  
                                     2012-03-06   TypeA   2.875"), header=TRUE, row.names=NULL)
    library(reshape2)
    dcast(df, PricingDate ~ Name)
    Using Rate as value column: use value.var to override.
      PricingDate TypeA TypeB TypeC TypeD TypeE
    1  2012-03-05 2.875  3.25  3.75  3.75   4.5
    2  2012-03-06 2.875    NA    NA    NA    NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for an elegant approach in PHP to take in a variable and then
I'm looking an elegant way to position two divs one besides the other without
I'm looking for a more elegant way to do something like this: [data[i/2] if
Im looking for an elegant way in Scala to split a given string into
I was looking for an elegant way to upload images AJAX style. I'm new
I'm looking for most elegant way to ajaxify my forms (with jQuery). How do
I'm looking for an elegant way to suppress exceptions when calling a method. I
I'm looking for an elegant way to generate a sequence of the rolling average
I'm looking for an elegant way to use values in a Java enum to
I'm looking for an elegant way to turn this array: Array ( [foo] =>

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.