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

StackOverflow https://stackoverflow.com/questions/2831319

  •  26-09-2019
  •  | 
  •  

문제

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

I'm getting this error:

Error in as.Date.numeric(value) : 'origin' must be supplied

and while I understand the error I'm not exactly sure how to get around it.

A small sample of the data is:

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.

도움이 되었습니까?

해결책

Try converting the created_at variable into a character vector. melt also doesn't seem to like the cast_df class, but I had success by resetting the class to just data.frame. Like so:

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

다른 팁

You error is caused by rbind used in melt, which is consequence of wrong data to melt. I don't know how you create your cast_df data.frame, but it missing attributes (idvars and rdimnames) which are required by melt.cast_df.

That is why wkmor1 solution works, melt.data.frame don't need this arguments. And without converting Date to character it can be done as:

df <- as.data.frame(df)
melt(df, id="created_at")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top