我有一个数据框,想知道某个字符串是否存在。我想知道df,1中的任何值是否包含来自inscompany的任何内容。
df = data.frame(company=c("KMart", "Shelter"), var2=c(5,7))
if( df[,1] == inscompany ) print("YES")
inscompany <- c("21st Century Auto Insurance", "AAA Auto Insurance", "AARP Auto Insurance",
"Allstate Auto Insurance", "American Family Auto Insurance", "Eastwood Auto Insurance",
"Erie Auto Insurance", "Farmers Auto Insurance", "GMAC Auto Insurance", "Hartford Auto Insurance",
"Infinity Auto Insurance", "Mercury Auto Insurance", "Nationwide Auto Insurance", "Progressive Auto Insurance",
"Shelter Insurance Company", "Titan Auto Insurance", "Travelers Auto Insurance", "USAA Auto Insurance")我收到一条错误消息,它只能将inscompany的第一个值检查为df,1。
帮助!
发布于 2011-06-04 04:13:19
你想要%in%。下面是一个示例:
R> chk <- c("A", "B", "Z") # some text
R> chk %in% LETTERS[1:13] # check for presence in first half of alphabet
[1] TRUE TRUE FALSE
R> match()函数是相关的,有关详细信息,请参阅帮助页面。
发布于 2011-06-04 10:36:32
我认为match和%in%不适用于部分匹配。grepl根据目标字符串是否包含给出逻辑(TRUE/FALSE)结果;我使用^只在字符串的开头进行匹配(您可能不需要这样做)。要扩展到多对多匹配,需要any和sapply。如果您只想知道是否有任何字符串匹配,则需要在整个过程中再添加一个any。
sapply(df$company,function(x) any(grepl(paste("^",x,sep=""),inscompany)))
[1] FALSE TRUEhttps://stackoverflow.com/questions/6232108
复制相似问题