dataframe - R: Question about table reshaping -
i have following data frame:
id,property1,property2,property3 1,1,0,0 2,1,1,0 3,0,0,1 4,1,1,1 d.f <- structure(list(id = 1:4, property1 = c(1l, 1l, 0l, 1l), property2 = c(0l, 1l, 0l, 1l), property3 = c(0l, 0l, 1l, 1l)), .names = c("id", "property1", "property2", "property3"), class = "data.frame", row.names = c(na, -4l)) what least cumbersome way following data frame:
id,properties_list 1,property1 2,property1, property2 3,property3 4,property1, property2, property3 maybe melt or reshape fancy options?
this solution assumes you're looking data frame similar how gsk3 interpreted question (pasting properties together) obligatory avoidance of for loop, cause that's how roll r:
property_list <- apply(d.f[,-1],1, fun=function(x,nms){paste(nms[as.logical(x)],collapse=",")}, nms=colnames(d.f)[-1]) as.data.frame(cbind(d.f$id,property_list)) v1 property_list 1 1 property1 2 2 property1,property2 3 3 property3 4 4 property1,property2,property3
Comments
Post a Comment