How to resolve dcast error with reshape2 package i

2019-02-23 08:41发布

问题:

I'm trying to convert some data from long to wide using the reshape2 package, but I'm getting an error that I haven't been able to resolve. In the example below, I've created some fake data that's similar to my real data. I want to convert each "subj" to a column that contains the sum of all "credits" in that subject for a given "ID" number.

library(reshape2)  

# Create fake data and turn it into a data frame

ID = rep(c(100,101,102,103), each=5)  
subj = rep(c("CHEM","ENGL","HIST","MATH"), 5)  
credits = rep(3, 20)  
df = data.frame(ID, subj, credits)

# Convert from long to wide, with the values of "subj" as the new columns
# and the sum of "credits" as the values for each "subj"

df.wide = dcast(df, ID ~ subj, value.var=credits, fun.aggregate=sum)

Here's what I'm trying to get when I run the dcast command:

ID    CHEM    ENGL    HIST    MATH  
100     6       3       3      3  
101     3       6       3      3  
[and so on for each value of ID]  

Here's the error I actually get when I run the code above:

Error in .subset2(x, i, exact = exact) : 
recursive indexing failed at level 2

I get the same error if I remove "fun.aggregate=sum" from the dcast call.

Also, if I create the values for "credits" using the sample() function (instead of rep()), and call dcast (without fun.aggregate=sum), I get the following error:

Error in .subset2(x, i, exact = exact) : 
no such index at level 1

I've run what are, as far as I can tell, similar dcast commands before without a problem. I'm guessing I'm going to slap my forehead and shout "doh" when I see the solution, but I'm stuck.

回答1:

Just put credits in quotation marks:

df.wide = dcast(df, ID ~ subj, value.var="credits", fun.aggregate=sum)
df.wide
   ID CHEM ENGL HIST MATH
1 100    6    3    3    3
2 101    3    6    3    3
3 102    3    3    6    3
4 103    3    3    3    6


标签: r reshape