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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:26:07+00:00 2026-06-11T23:26:07+00:00

I must be missing something trivial but I can’t see why the below cast

  • 0

I must be missing something trivial but I can’t see why the below cast converts my dataframe to a list. I would like the output to be a dataframe if possible.

It starts as

 str(d)
     'data.frame':   12 obs. of  4 variables:
     $ credit_id: num  12 12 12 12 18 ...
     $ Date     : Date, format: "2003-06-30" "2003-09-30" "2003-12-31" ...
     $ value    : num  840 854 847 834 4831 ...
     $ varb     : chr  "sales" "sales" "sales" "sales" ...

then I try to cast it

d<-cast(d,Date+credit_id~varb)

and I get

str(d)

    List of 4
     $ Date     : Date[1:9], format: "2003-06-30" "2003-06-30" "2003-09-30" ...
     $ credit_id: num [1:9] 12 18 12 18 12 ...
     $ ebitda   : num [1:9] NA NA NA NA NA ...
     $ sales    : num [1:9] 840 4831 854 4670 847 ...
     - attr(*, "row.names")= int [1:9] 1 2 3 4 5 6 7 8 9
     - attr(*, "idvars")= chr [1:2] "Date" "credit_id"
     - attr(*, "rdimnames")=List of 2
      ..$ :'data.frame':    9 obs. of  2 variables:
      .. ..$ Date     : Date[1:9], format: "2003-06-30" "2003-06-30" "2003-09-30" ...
      .. ..$ credit_id: num [1:9] 12 18 12 18 12 ...
      ..$ :'data.frame':    2 obs. of  1 variable:
      .. ..$ varb: chr [1:2] "ebitda" "sales"

Full code below.
Thanks in advance.

    d<-structure(list(credit_id = c(12, 12, 12, 12, 18, 18, 2073, 2073,
    2103, 2103, 1776, 1776), Date = structure(c(12233, 12325, 12417,
    12508, 12233, 12325, 15552, 15552, 15552, 15552, 15552, 15552
    ), class = "Date"), value = c(839.8, 853.9, 846.9, 833.7, 4831.2,
    4670, 54.1, 995, 90.944, 1092.8, 81.2, 1348.2), varb = c("sales",
    "sales", "sales", "sales", "sales", "sales", "ebitda", "sales",
    "ebitda", "sales", "ebitda", "sales")), .Names = c("credit_id",
    "Date", "value", "varb"), row.names = c(606799L, 606800L, 606801L,
    606802L, 606805L, 606806L, 1131814L, 1131822L, 1131950L, 1131958L,
    1132034L, 1132042L), class = "data.frame")
    head(d)
    str(d)
    d<-cast(d,Date+credit_id~varb)
    head(d)
    str(d)
  • 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-11T23:26:08+00:00Added an answer on June 11, 2026 at 11:26 pm

    Use dcast from reshape2 package. From ?dcast you can realize that dcast gives you a dataframe as output.

    set.seed(001) # Generating some data.
    credit_id <- sample(c(12,14,13,11), 10, TRUE)
    Date  <- seq(Sys.Date(), length.out=10, by="1 day")
    value <- rnorm(10,1000,50)
    varb <- sample(c("ebitda", "sales"), 10, TRUE)
    
    d <- data.frame(credit_id, Date, value, varb) # this is something like your df
    str(d)
    
    'data.frame':   10 obs. of  4 variables:
     $ credit_id: num  14 14 13 11 12 11 11 13 13 12
     $ Date     : Date, format: "2012-09-27" "2012-09-28" ...
     $ value    : num  959 1024 1037 1029 985 ...
     $ varb     : Factor w/ 2 levels "ebitda","sales": 1 2 1 1 2 2 2 1 2 1
    
    d2 <- dcast(d,Date+credit_id~varb)
    str(d2)
    
    'data.frame':   10 obs. of  4 variables:
     $ Date     : Date, format: "2012-09-27" "2012-09-28" ...
     $ credit_id: num  14 14 13 11 12 11 11 13 13 12
     $ ebitda   : num  959 NA 1037 1029 NA ...
     $ sales    : num  NA 1024 NA NA 985 ...
    

    d and d2 look like:

    d
       credit_id       Date     value   varb
    1         14 2012-09-27  958.9766 ebitda
    2         14 2012-09-28 1024.3715  sales
    3         13 2012-09-29 1036.9162 ebitda
    4         11 2012-09-30 1028.7891 ebitda
    5         12 2012-10-01  984.7306  sales
    6         11 2012-10-02 1075.5891  sales
    7         11 2012-10-03 1019.4922  sales
    8         13 2012-10-04  968.9380 ebitda
    9         13 2012-10-05  889.2650  sales
    10        12 2012-10-06 1056.2465 ebitda
    
    d2
             Date credit_id    ebitda     sales
    1  2012-09-27        14  958.9766        NA
    2  2012-09-28        14        NA 1024.3715
    3  2012-09-29        13 1036.9162        NA
    4  2012-09-30        11 1028.7891        NA
    5  2012-10-01        12        NA  984.7306
    6  2012-10-02        11        NA 1075.5891
    7  2012-10-03        11        NA 1019.4922
    8  2012-10-04        13  968.9380        NA
    9  2012-10-05        13        NA  889.2650
    10 2012-10-06        12 1056.2465        NA
    

    Next time make sure to provide some data by using dput(dataframe) to make your question reproducible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like I must be missing something completely obvious, but I don't see
I must be missing something simple, but I can't see it. First, the setup:
I must be missing something stupid here but I can not see it. My
I feel like I must be missing something, but I just don't see what
I must be missing something very simple, but can't find the answer to this.
I think i must be missing something simple here but I can't figure out
I must be missing something obvious, but I can't make it work I want
I must be missing something obvious here, but I can't seem to get the
I must be missing something very obvious, but I've searched all over and can't
I must be missing something totally obvious but I can't seem to get the

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.