R reshape2 'Aggregation function missing: defaulting to length'
Thanks to @akrun who pointed it out.
Well, there's a high chance that your data has duplicate row that look either like this:
student test score
Adam Exam1 80
Adam Exam1 85
Adam Exam2 90
John Exam1 70
John Exam2 60
Or like this:
student class test score
Adam Biology Exam1 80
Adam Theology Exam1 85
Adam Theology Exam2 90
John Biology Exam1 70
John Theology Exam2 60
When you cast it like this: dcast(data, student + class ~ test, value.var='score')
Can dcast be used without an aggregate function?
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
Related Topics
Formatting Decimal Places in R
Select/Assign to Data.Table When Variable Names Are Stored in a Character Vector
How to Get Summary Statistics by Group
How to Trim Leading and Trailing White Space
Relative Frequencies/Proportions With Dplyr
Plot Two Graphs in Same Plot in R
How to Use R'S Ellipsis Feature When Writing Your Own Function
How to Create a Lag Variable Within Each Group
Data.Table VS Dplyr: Can One Do Something Well the Other Can't or Does Poorly
Convert Data.Frame Columns from Factors to Characters
Complete Dataframe With Missing Combinations of Values
Finding Local Maxima and Minima
Counting the Number of Elements With the Values of X in a Vector
Difference Between Require() and Library()
Divide All Columns by the Value from the 2Nd Column - Apply for All Rows
How to Add Row and Column to a Dataframe of Different Length