Reshape DF from long to wide in R using Reshape2 without an aggregation function

懵懂的女人 提交于 2019-12-11 10:13:24

问题


a common task in the data I work with is reshaping client data from long to wide. I have a process to do this with Reshape outlined below that basically creates new (but unmodified) columns with a numeric index appended. In my case I do not want to perform any modifications on the data. My question, because I often use reshape2 for other operations, is how this can be accomplished with dcast? It does not seem that the example data need to be melted by id, for example, but I'm not sure how I would go about making it wide. Would anyone be able to provide code in reshape2 to produce a frame comparable to "wide" in the example below?

Thanks.

Example

date_up   <- as.numeric(as.Date("1990/01/01"))
date_down <- as.numeric(as.Date("1960/01/01"))
ids <- data.frame(id=rep(1:1000, 3),site=rep(c("NMA", "NMB","NMC"), 1000))
ids <- ids[order(ids$id), ]
dates <-  data.frame(datelast=runif(3000, date_down, date_up),
          datestart=runif(3000, date_down, date_up),
          dateend=runif(3000, date_down, date_up),
          datemiddle=runif(3000, date_down, date_up))
dates[] <- lapply(dates[ , c("datestart", "dateend", "datemiddle")], 
             as.Date.numeric, origin = "1970-01-01")
df <- cbind(ids, dates)

# Make a within group index and reshape df
df$gid <- with(df, ave(rep(1, nrow(df)), df[,"id"], FUN = seq_along))
wide <- reshape(df, idvar = "id", timevar = "gid", direction = "wide")

回答1:


We can use dcast from data.table, which can take multiple value.var columns. Convert the 'data.frame' to 'data.table' (setDT(df)), use the dcast with formula and value.var specified.

library(data.table)
dcast(setDT(df), id~gid, value.var=names(df)[2:6])

NOTE: The data.table method would be faster compared to the reshape2



来源:https://stackoverflow.com/questions/35000367/reshape-df-from-long-to-wide-in-r-using-reshape2-without-an-aggregation-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!