dcast converts characters to factors when margins are given #8

mhofert opened this Issue Aug 17, 2011 · 1 comment


None yet
2 participants

mhofert commented Aug 17, 2011

Dear Hadley,

I realized a strange behavior with characters being converted to factors by dcast()
but only if "margins" is given:


df <- data.frame(Time=0:11, Group=rep(c("Group.1","Group.2","Group.3"), each=4), 
                 Value=1:12, stringsAsFactors=FALSE)
sapply(1:3, function(j) class(df[,j])) # okay, fine

df. <- dcast(df, Time + Group ~ "Value", fun.aggregate=sum, value_var="Value") 
sapply(1:3, function(j) class(df.[,j])) # okay, still fine

df.. <- dcast(df, Time + Group ~ "Value", fun.aggregate=sum, value_var="Value", margins="Group") 
sapply(1:3, function(j) class(df..[,j])) # problem: the Group column is converted to factor

It might be due to some newly created data.frame... Maybe dcast should accept a
stringsAsFactors argument?



PS: Another "nice-to-have" would be if the user could provide an alternative string for "(all)" when creating margins.


hadley commented Mar 10, 2014

Hi Marius,

This is by design - otherwise the margin value "(all)" wouldn't sort at the end.


hadley closed this Mar 10, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment