我很抱歉,因为这似乎是一个基本问题,但我一直在寻找更好的解决方案,但没有找到。我有以下类型的数据。
myDATA<-data.frame(rbind(c("red","blue","green", "dog","hat","cat")
,c("blue","green", "blue","dog","hat","cat")
,c("green","blue","blue","dog","hat","cat")
,c("green","red", "blue","dog","hat","cat")
)
)
names(myDATA)<-c(paste("Color",1:3,sep=""),paste("Stim",1:3,sep=""))
myDATA$greenImage<-NA
这使:
我的数据
+-----------------------------------------------------+
| Color1 Color2 Color3 Stim1 Stim2 Stim3 greenImage |
+-----------------------------------------------------+
| 1 red blue green dog hat cat NA |
| 2 blue green blue dog hat cat NA |
| 3 green blue blue dog hat cat NA |
| 4 green red blue dog hat cat NA |
+-----------------------------------------------------+
列Color
与Stim
列按编号对应,例如Stim1
显示在Color1
等。对于每一行,一个Stim
以绿色显示。我想找到它Stim
并保存在一个名为greenImage
.
我从一些在这里可能有用的帖子中收集到,apply()
但我无法让它发挥作用。我相当不雅的解决方案是以下形式的循环,
for (i in 1:nrow(myDATA)) {
x <- match("green", unlist(myDATA[i,paste("Color", 1:3, sep="")]))
myDATA[i,"greenImage"] <- as.character(myDATA[i, paste("Stim", x, sep="")])
}
导致:
myDATA
+-----------------------------------------------------+
| Color1 Color2 Color3 Stim1 Stim2 Stim3 greenImage |
+-----------------------------------------------------+
| 1 red blue green dog hat cat cat |
| 2 blue green blue dog hat cat hat |
| 3 green blue blue dog hat cat dog |
| 4 green red blue dog hat cat dog |
+-----------------------------------------------------+
但是,实际数据集有超过 10000 行,所以我的解决方案效率很低。任何人都可以提出一种更有效的替代方法吗?
提前致谢!