0

我很抱歉,因为这似乎是一个基本问题,但我一直在寻找更好的解决方案,但没有找到。我有以下类型的数据。

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 |
+-----------------------------------------------------+

ColorStim列按编号对应,例如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 行,所以我的解决方案效率很低。任何人都可以提出一种更有效的替代方法吗?

提前致谢!

4

1 回答 1

1

只需用于ifelse矢量化您的比较:

for (i in 1:3) {
  myDATA$greenImage = ifelse (myDATA[,i] == "green",
                              as.character(myDATA[,i+3]),
                              myDATA$greenImage)
}

请注意,将字符串从's 中as.character取出是必需的。factor如果您stringsAsFactors = FALSE在创建data.frame.

于 2013-04-25T18:14:52.670 回答