移调列5-8,每次在3列2,或4行值的变化

问题描述 投票:-1回答:1

这里是我的数据集enter image description here

我所试图做的是每次有新管时转过去的4列(或新的殖民地或新样本)因此转化成这样的某物:enter image description here

我一直在试图做到这一点在R,但我接受其他的建议,任何帮助是非常感谢!

r conditional transpose
1个回答
0
投票

OK找到了解决方法(对于那些谁可能在今后类似的问题)

在下面的示例数据,我假定列2和3必须保持,而第4-6栏需要被转置。

我用“拆散”这使得任务很容易,可能有解决这个更简单的方法,但这个工作相当确定了我,所以我会先不管它。

create data

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)

conversion

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)
© www.soinside.com 2019 - 2024. All rights reserved.