初级问题来了,对不起......我有与结构如下数据集:重新排序的数据帧到基于行的名称列[R
dat.1<-data.frame(id=c(1,1,1,2,2,2),test=c("test.1","test.2","test.3"),result=c(1,2,1,2,2,1))
dat.1
id test result
1 1 test.1 1
2 1 test.2 2
3 1 test.3 1
4 2 test.1 2
5 2 test.2 2
6 2 test.3 1
实际数据集目前有32次测试,1000 < ID号码,结果总是二进制的 - 测试数量可以增加,ID也会增加。我想重新安排数据,使每个测试即“test.1”有像这样的列:
dat.3<-data.frame(id=c(1,2),test.1=c(1,2),test.2=c(2,2),test.3=c(1,1))
dat.3
id test.1 test.2 test.3
1 1 1 2 1
2 2 2 2 1
这方面的一个小麻烦是,并非每一个ID已经发生了各项测试,所以任何解决方案将必须应付NA。只是在dat.3中澄清,测试的列内容是dat.1的结果列。
在据我已经得到了作为创建一个“空”的数据帧,可以适应新的考验的时刻被添加像这样:
dat.2<-data.frame(id=c(1,2),test.1=c(NA,NA),test.2=c(NA,NA),test.3=c(NA,NA))
dat.2
id test.1 test.2 test.3
1 1 NA NA NA
2 2 NA NA NA
我一直在尝试与ifelse与IF的逻辑dat.1 $ id == dat.2 $ id & dat.1 $ test ==“test.1”then where dat.2 col = test.1,输入dat.1 $ result in dat.3 $ test.1 - 如果这有什么意义的话!可以预见,没有任何运气,感觉我错过了一个非常明显的步骤/过于复杂的事情,所以任何帮助将不胜感激 - 谢谢
编辑:感谢您的意见 - 重塑已开始有所帮助;不过,我认为我试图用上面的例子过度简化。我已经把下方的新示例数据集:
dat.4<-data.frame(id=c(1,1,1,1,1,1,2,2,2),result=c(1,1,1,2,2,2,3,3,3),
test=c("test.1","test.2","test.3"),result=c(1,2,1,2,2,2,2,2,1))
dat.1
id result test result.1
1 1 1 test.1 1
2 1 1 test.2 2
3 1 1 test.3 1
4 1 2 test.1 2
5 1 2 test.2 2
6 1 2 test.3 2
7 2 3 test.1 2
8 2 3 test.2 2
9 2 3 test.3 1
因此,每个ID(实际上是一个试样ID)有其有合格的用于这项进一步测试的测试 - 此测试可具有单个或多个结果。因此,在最终的数据结构上面的例子是这样的:
dat.3<-data.frame(id=c(1,1,2),result=c(1,2,3),test.1=c(1,2,2),test.2=c(2,2,2),
test.3=c(1,2,1))
dat.3
id result test.1 test.2 test.3
1 1 1 1 2 1
2 1 2 2 2 2
3 2 3 2 2 1
所以真的是我要寻找基于两个列条件重塑 - 这是否有道理?
感谢。你可以根据两列内容重塑吗?在解决方案之前没有预料到这个问题 - 将编辑原始问题进一步解释 –