我正在尝试用* ply类型函数替换for循环。
我遇到的问题是我不确定如何重复更新相同的数据。
以下是一些示例数据(我知道这个具体示例可以通过其他方式完成,但这只是为了简单起见 - 我的真实示例要复杂得多):
sample_pat_rep <- data.frame(matrix(NA, ncol=2, nrow=3, dimnames=list(c(), c("Pattern","Replacement"))), stringsAsFactors=FALSE)
sample_pat_rep[1,] <- c("a","A")
sample_pat_rep[2,] <- c("b","B")
sample_pat_rep[3,] <- c("c","C")
sample_strings <- data.frame(matrix(NA, ncol=2, nrow=3, dimnames=list(c(), c("Original","Fixed"))), stringsAsFactors=FALSE)
sample_strings[1,] <- c("aaaaaaaa bbbbbbbb cccccccc","aaaaaaaa bbbbbbbb cccccccc")
sample_strings[2,] <- c("aAaAaAaA bBbBbBbB cCcCcCcC","aAaAaAaA bBbBbBbB cCcCcCcC")
sample_strings[3,] <- c("AaAaAaAa BbBbBbBb CcCcCcCc","AaAaAaAa BbBbBbBb CcCcCcCc")
这是一个for循环版本:
sample_strings1 <- sample_strings
for (i in 1:nrow(sample_pat_rep))
{
sample_strings1[,c("Fixed")] <- gsub(sample_pat_rep[i,c("Pattern")], sample_pat_rep[i,c("Replacement")], sample_strings1[,c("Fixed")],ignore.case = TRUE)
}
当我尝试用adply复制它时,它不会更新数据 - 它必须复制并重新绑定它。
sample_strings2 <- adply(.data=sample_pat_rep, .margins=1, .fun = function(x,data){
data[,c("Fixed")] <- gsub(x[,c("Pattern")], x[,c("Replacement")], data[,c("Fixed")],ignore.case = TRUE)
return(data)
}, data=sample_strings, .expand = FALSE, .progress = "none", .inform = FALSE, .parallel = FALSE, .paropts = NULL)
我确定有一个简单的解决方法。我看了拉普利,但不清楚这是解决方法。
也许写一个函数来调用?使用Rapply ??
提前致谢!
更新:新数据
这更接近实际情况。匹配是动态的,基于外部系统。我试图避免过于复杂的正则表达式或嵌套if ifses。
library(plyr)
sample_match <- data.frame(matrix(NA, ncol=1, nrow=3, dimnames=list(c(), c("Match"))), stringsAsFactors=FALSE)
sample_match[1,] <- c("dog")
sample_match[2,] <- c("cat")
sample_match[3,] <- c("bear")
sample_strings <- data.frame(matrix(NA, ncol=2, nrow=3, dimnames=list(c(), c("Sentence","Has_Animal"))), stringsAsFactors=FALSE)
sample_strings[1,] <- c("This person only has a cat",0)
sample_strings[2,] <- c("This person has a cat and a dog",0)
sample_strings[3,] <- c("This person has no animals",0)
sample_strings1 <- sample_strings
for (i in 1:nrow(sample_match))
{
sample_strings1[,c("Has_Animal")] <- ifelse(grepl(sample_match[i,c("Match")], sample_strings1[,c("Sentence")]), 1,sample_strings1[,c("Has_Animal")])
}
sample_strings2 <- adply(.data=sample_match, .margins=1, .fun = function(x,data){
data[,c("Has_Animal")] <- ifelse(grepl(x[,c("Match")], data[,c("Sentence")]), 1,data[,c("Has_Animal")])
return(data)
}, data=sample_strings, .expand = FALSE, .progress = "none", .inform = FALSE, .parallel = FALSE, .paropts = NULL)
答案 0 :(得分:0)
更新:误解了问题,sample_strings2
是必需的结果。更新了现在提供sample_strings1
的答案,其中IIUC是必需的。
以下是使用base
的解决方案:
pattern = paste(sample_match$Match, collapse="|")
transform(sample_strings, Has_Animal = grepl(pattern, Sentence)*1L)
# Sentence Has_Animal
# 1 This person only has a cat 1
# 2 This person has a cat and a dog 1
# 3 This person has no animals 0
如果您不想匹配包含该模式的字词,例如:concatenate
包含cat
,那么您可以将正则表达式\b
用于字边界。
pattern = paste(paste("\\b", sample_match$Match, "\\b", sep=""), collapse="|")
grepl(pattern, c("cat", "concatenate"))
# [1] TRUE FALSE
答案 1 :(得分:0)
这是问题的直接plyr
方法:
ddply(sample_strings,.(Sentence),function(x,ref = sample_match) {
any(unlist(strsplit(x[["Sentence"]]," ")) %in% ref[[1]])
})
Sentence V1
1 This person has a cat and a dog TRUE
2 This person has no animals FALSE
3 This person only has a cat TRUE