如何解决R从长到宽数据框,带有实值列
我有一个快速问题,涉及到重塑我的ID为“ grouped_by”数据的数据框。 我有以下df模式(我希望扩大2个示例性实例(总计我有> 5000个)):
id solver scoreA scoreB group size
<chr> <chr> <dbl> <dbl> <chr> <dbl>
1 instance_1 s1 1 0.5 g1 1000
2 instance_1 s2 100 50 g1 1000
...我想获得的是:
id solver.best scoreA.s1 scoreA.s2 scoreB.s1 scoreB.s2 group size
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <dbl>
1 instance_1 s1 1 100 0.5 50 g1 1000
感谢您的帮助。 BR
解决方法
也许您可以尝试下面的代码
reshape(within(df,Q <- ave(seq(nrow(df)),id,FUN = seq_along)),direction = "wide",idvar = "id",timevar = "Q"
)
给出
> reshape(cbind(df,Q = seq(nrow(df))),timevar = "Q")
id solver.1 scoreA.1 scoreB.1 group.1 size.1 solver.2 scoreA.2
1 instance 1 s1 1 0.5 g1 1000 s2 100
scoreB.2 group.2 size.2
1 50 g1 1000
数据
> dput(df)
structure(list(id = c("instance 1","instance 1"),solver = c("s1","s2"),scoreA = c(1L,100L),scoreB = c(0.5,50),group = c("g1","g1"),size = c(1000L,1000L)),class = "data.frame",row.names = c("1","2"))
,
我仍然希望有一个方便的选择,例如tidyverse,最佳实践,我仍然想分享实用的方法,该方法在概念上同样有效:):
# create empty (wide) target df
wide_df <- data.frame(matrix(ncol = 8,nrow = 0))
names <- c("id","best_solver","scoreA_s1","scoreA_s2","scoreB_s1","scoreB_s2","group","size")
colnames(wide_df) <- names
# traverse grouped by and arranged original (long) df
for(i in seq(2,length(long_df$group),by = 2)){
wide_df[i/2,"id"] <- long_df[i,"id"]
wide_df[i/2,"best_solver"] <- long_df[which(long_df[(i-1):i,"scoreA"] ==
min(long_df[i-1,"scoreA"],long_df[i,"scoreA"])),"solver"]
wide_df[i/2,"scoreA_s1"] <- long_df[i-1,"scoreA"]
wide_df[i/2,"scoreA_s2"] <- long_df[i,"scoreB_s1"] <- long_df[i-1,"scoreB"]
wide_df[i/2,"scoreB_s2"] <- long_df[i,"group"] <- long_df[i,"group"]
wide_df[i/2,"size"] <- long_df[i,"size"]
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。