【发布时间】:2017-05-13 18:04:25
【问题描述】:
每个 memberID 我的数据有多行。我在下面模拟了它(请参阅#this is what I have)。我需要将其转换为每个 memberID 有一行的形状,创建多列。我也在下面模拟了所需的输出(请参阅#this is what I want)。我正在寻找在 R 中进行这种转换的最简单方法。我是一个全新的 R 和 RStudio 用户,几乎没有编码背景。非常感谢您提供代码方面的帮助。
#this is what I have
ID <- c(1,1,2,3,3,3)
Date <- as.Date (c("2015/01/01","2016/01/03", "2011/03/01", "2015/01/09", "2017/12/11","2016/09/09" ))
Score <- c(5,15,2,6,12,18)
df <- data.frame(ID, Date, Score)
df
#this is what i want
ID2 <- c(1,2,3)
Date1 <- as.Date (c("2015/01/01","2011/03/01","2015/01/09"))
Date2 <- as.Date (c("2016/01/03", NA, "2017/12/11"))
Date3 <- as.Date (c(NA, NA,"2016/09/09"))
Score1 <- c(5,2,6)
Score2 <- c(15,NA,12)
Score3 <- c("NA","NA",18)
df2 <- data.frame (ID2, Date1, Date2, Date3, Score1, Score2, Score3)
df2
【问题讨论】: