在以下数据框中,我想创建一个新变量作为所有现有变量的以下函数:
as.numeric(paste0(df[i,],collapse=""))
但是,我不想明确定义列名,因为它们的编号和名称每次都可能不同。我怎么能用dplyr做到这一点?
基数r中的等价物将是这样的:
apply(df,1,function(x) as.numeric(paste0(x,collapse="")))
df <- structure(list(X1 = c(50, 2, 2, 50, 5, 5, 2, 50, 5, 5, 50, 2,
5, 5, 50, 2, 2, 50, 9, 9, 9, 9, 9, 9), X2 = c(2, 50, 5, 5, 50,
2, 5, 5, 50, 2, 2, 50, 9, 9, 9, 9, 9, 9, 50, 2, 2, 50, 5, 5),
X3 = c(5, 5, 50, 2, 2, 50, 9, 9, 9, 9, 9, 9, 50, 2, 2, 50,
5, 5, 2, 50, 5, 5, 50, 2), X4 = c(9, 9, 9, 9, 9, 9, 50, 2,
2, 50, 5, 5, 2, 50, 5, 5, 50, 2, 5, 5, 50, 2, 2, 50)), class = "data.frame", .Names = c("X1",
"X2", "X3", "X4"), row.names = c(NA, -24L))
答案 0 :(得分:3)
您可以尝试:
{ "_id" : ObjectId("55698e906e6172193f000001"), "id" : 5678, "message" : "Hey, how are you", "type" => 1, "timestamp" => 4567723456789 }
或(如您所说,可能更多{ "_id" : ObjectId("5561d03f7979393062b18bf9"), "id" : 5678, "message" : "I am good", "type" => 2, "timestamp" => 654313687989 }
样式):
df %>% mutate(newcol=as.numeric(do.call(paste0,df)))