【发布时间】:2015-02-05 10:32:58
【问题描述】:
我必须编码很多data.frames。例如:
tt <- data.frame(V1=c("test1", "test3", "test1", "test4", "wins", "loses"),
V2=c("someannotation", "othertext", "loads of text including the word winning for the winner and the word losing for the loser", "blablabla", "blablabla", "blablabla"))
tt
V1 V2
test1 someannotation
test3 othertext
test1 loads of text including the word winning for the winner and the word losing for the loser
test4 blablabla
wins blablabla
loses blablabla
编码必须进入一个新的data.frame,如果跑步者获胜或失败,我必须编码。如果V1 表示wins,那么他赢了(如果他输了,则用loses 表示)。但是,跑步者有可能赢得或输掉比赛的一部分,这由V1 中的test1 表示,并由V2 指定。如果V2 中的术语winning 出现在术语losing 之前,则跑步者将赢得部分比赛(反之亦然)。
我尝试从这里实现答案元素以指定哪个单词/字符串出现在哪个位置:
find location of character in string
实现如下所示:
result <- data.frame()
for(i in 1:length(tt[,1])){
if(grepl("wins", tt[i,1])) result[i,1] <- "wins"
if(grepl("loses", tt[i,1])) result[i,1] <- "loses"
if(grepl("test1", tt[i,1])&(which(strsplit(tt[i,2], " ")[[1]]=="winning")>which(strsplit(tt[i,2], " ")[[1]]=="losing"))) result[i,1] <- "loses"
if(grepl("test1", tt[i,1])&(which(strsplit(tt[i,2], " ")[[1]]=="winning")<which(strsplit(tt[i,2], " ")[[1]]=="losing"))) result[i,1] <- "wins"
}
但是对于不包含winning 或losing 的V2 列的单元格有错误消息:
Error in if (grepl("test1", tt[i, 1]) & (which(strsplit(tt[i, 2], " ")[[1]] == : argument is of length zero
有没有人能解决这个问题,甚至是复杂的解决方案?任何帮助表示赞赏,谢谢!
编辑
正如@grrgrrbla 澄清的那样,有两种可能获胜:一种是如果V1 == "win",另一种是如果V2 在“失败”一词之前包含“获胜”一词,则跑步者也获胜,有两种可能会失败: V1 == "loses" 或 V2 在“获胜”之前包含“失败”。
我的输出应该是这样的:
result
V1
NA
NA
wins
NA
wins
loses
【问题讨论】:
-
请指定你想要的输出:一列,两列,你只需要一列说赢/输吗,你需要索引等等。所以据我所知,有两种可能性赢:一种是如果V1 ==“赢”,另一种是如果V2在“输”之前包含“赢”这个词,跑步者也赢了,有两种可能输:V1 ==“输”或V2包含在“赢”之前先“输”,对吧?输出应该是一列“赢”或“输”,对吧?
-
为什么输出中有 NA 值?什么时候应该出现 NA 值?结果应该给什么输入?
-
由于只有第 3、5 和 6 行包含“赢/赢”或“输/输”,其他行的编码应为
NA。