OK找到了解决方法(对于那些谁可能在今后类似的问题)
在下面的示例数据,我假定列2和3必须保持,而第4-6栏需要被转置。
我用“拆散”这使得任务很容易,可能有解决这个更简单的方法,但这个工作相当确定了我,所以我会先不管它。
x <- data.frame("SN" = 1:24, "Tube" = c(1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,5,5,5,5,5,5,5,5),
"Name" = c("John","John","John","John","John","John","John","John","Dora","Dora","Dora","Dora","Dora","Dora","Dora","Dora","Nick","Nick","Nick","Nick","Nick","Nick","Nick","Nick"))
x$CP1 <- sample(40, size = nrow(x), replace = TRUE)
x$CP2 <- sample(40, size = nrow(x), replace = TRUE)
x$CP3 <- sample(40, size = nrow(x), replace = TRUE)
tmp1 <- data.frame(CP1=x$CP1, ind=rep(1:8, nrow(x)/8))
tmp2 <- data.frame(CP2=x$CP2, ind=rep(1:8, nrow(x)/8))
tmp3 <- data.frame(CP3=x$CP3, ind=rep(1:8, nrow(x)/8))
new1=unstack(tmp1, CP1~ind)
new2=unstack(tmp2, CP2~ind)
new3=unstack(tmp3, CP3~ind)
z=unique(x[2:3])
tmp1=cbind(z,new1)
tmp2=cbind(z,new2)
tmp3=cbind(z,new3)
new.data=rbind(tmp1,tmp2,tmp3)