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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:17:28+00:00 2026-06-12T16:17:28+00:00

Possible Duplicate: This R reshaping should be simple, but dcast from reshape2 works without

  • 0

Possible Duplicate:
This R reshaping should be simple, but

dcast from reshape2 works without a formula where there are no duplicates. Take these example data:

df <- structure(list(id = c("A", "B", "C", "A", "B", "C"), cat = c("SS", 
"SS", "SS", "SV", "SV", "SV"), val = c(220L, 222L, 223L, 224L, 
225L, 2206L)), .Names = c("id", "cat", "val"), class = "data.frame", row.names = c(NA, 
-6L))

I’d like to dcast these data and just have the values tabulated, without applying any function to the value.var including the default length.

In this case, it works fine.

> dcast(df, id~cat, value.var="val")
  id  SS   SV
1  A 220  224
2  B 222  225
3  C 223 2206

But when there are duplicate variables, the fun defaults to length. Is there a way to avoid it?

df2 <- structure(list(id = c("A", "B", "C", "A", "B", "C", "C"), cat = c("SS", 
"SS", "SS", "SV", "SV", "SV", "SV"), val = c(220L, 222L, 223L, 
224L, 225L, 220L, 1L)), .Names = c("id", "cat", "val"), class = "data.frame", row.names = c(NA, 
-7L))

> dcast(df2, id~cat, value.var="val")
Aggregation function missing: defaulting to length
  id SS SV
1  A  1  1
2  B  1  1
3  C  1  2

Ideally what I’m looking for is to add a fun = NA, as in don’t try to aggregate the value.var. The result I’d like when dcasting df2:

 id  SS  SV
1  A 220 224
2  B 222 225
3  C 223 220
4. C NA  1
  • 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-12T16:17:30+00:00Added an answer on June 12, 2026 at 4:17 pm

    I don’t think there is a way to do it directly but we can add in an additional column which will help us out

    df2 <- structure(list(id = c("A", "B", "C", "A", "B", "C", "C"), cat = c("SS", 
    "SS", "SS", "SV", "SV", "SV", "SV"), val = c(220L, 222L, 223L, 
    224L, 225L, 220L, 1L)), .Names = c("id", "cat", "val"), class = "data.frame", row.names = c(NA, 
    -7L))
    
    library(reshape2)
    library(plyr)
    # Add a variable for how many times the id*cat combination has occured
    tmp <- ddply(df2, .(id, cat), transform, newid = paste(id, seq_along(cat)))
    # Aggregate using this newid and toss in the id so we don't lose it
    out <- dcast(tmp, id + newid ~ cat, value.var = "val")
    # Remove newid if we want
    out <- out[,-which(colnames(out) == "newid")]
    > out
    #  id  SS  SV
    #1  A 220 224
    #2  B 222 225
    #3  C 223 220
    #4  C  NA   1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: F#: This expression should have type 'unit', but has type 'ConsoleKeyInfo' I'm
Possible Duplicate: Is there a “Set” data structure in .Net? Duplicate: this is a
Possible Duplicate: Why is this “min” template of cpp-next at fault? From another question
Possible Duplicate: Reference - What does this symbol mean in PHP? Is there any
Possible Duplicate: Priority queue in .Net This question is similar, but i want to
Possible Duplicate: How does this “size of array” template function work? Is there any
Possible Duplicate: Why ‘this’ is a pointer and not a reference? Is there a
Possible Duplicate: Is this a good way to generate a string of random characters?
Possible Duplicate: How does this CSS triangle shape work? Please help me i need
Possible Duplicate: Why do this Ruby object have two to_s and inspect methods that

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.