I am getting an error when trying to use melt () on a dataframe containing dates

I would like to melt a dataframe so that in one column I have dates in the second, I have the username as a variable and finally the value.

I am getting this error:

Error in as.Date.numeric (string): "source" must be sent

and while I understand the error, I'm not really sure how to work around it.

Small sample data:

structure(list(created_at = structure(c(14007, 14008, 14009, 
14010, 14011, 14012), class = "Date"), benjamin = c(16, 0, 0, 
0, 0, 0), byron = c(0, 0, 0, 0, 0, 0), cameronc = c(0, 0, 0, 
0, 0, 0), daniel = c(0, 0, 0, 0, 0, 0), djdiaz = c(0, 0, 0, 0, 
0, 0), gene = c(16, 77, 64, 38, 72, 36), joel = c(0, 0, 0, 0, 
0, 2), kerem = c(0, 0, 0, 0, 0, 0), sophia = c(0, 0, 0, 0, 0, 
0), SuperMoonMan = c(0, 0, 0, 0, 0, 0)), .Names = c("created_at", 
"benjamin", "byron", "cameronc", "daniel", "djdiaz", "gene", 
"joel", "kerem", "sophia", "SuperMoonMan"), row.names = c(NA, 
6L), class = c("cast_df", "data.frame"))

      

Thanks for your help.

+2


a source to share


2 answers


Try converting the variable created_at

to a character vector. melt

also doesn't look like a class cast_df

, but I have had success dropping the class on only data.frame

. For instance:



df <- as.data.frame(df)
df$created_at <- as.character(df$created_at)
library(reshape)
melt(df)

      

+4


a source


The error is caused by the rbind

used in melt

, which is the result of incorrect data for melting. I do not know how you create your cast_df

data.frame

, but there are no attributes ( idvars

and rdimnames

) that are required melt.cast_df

.

This is why wkmor1 works, melt.data.frame

these arguments are not needed. And without converting Date

to character

this, you can do it like:



df <- as.data.frame(df)
melt(df, id="created_at")

      

+2


a source







All Articles