R:字符串上的ifelse

时间:2013-07-25 18:45:14

标签: string r if-statement

我正在根据另一个变量的字符串条件填充数据框的新变量。我收到以下错误消息:

Error in Source == "httpWWW.BGDAILYNEWS.COM" | Source == : operations are possible only for numeric, logical or complex types

我的代码如下:

County <- ifelse(Source == 'httpWWW.BGDAILYNEWS.COM' | 'WWW.BGDAILYNEWS.COM', 'Warren', ifelse(Source == 'httpWWW.HCLOCAL.COM' | 'WWW.HCLOCAL.COM', 'Henry', ifelse(Source == 'httpWWW.KENTUCKY.COM' | 'WWW.KENTUCKY.COM', 'Fayette', ifelse(Source == 'httpWWW.KENTUCKYNEWERA.COM' | 'WWW.KENTUCKYNEWERA.COM', 'Christian') )))

2 个答案:

答案 0 :(得分:6)

我建议你将深层嵌套的ifelse语句分解为更易于管理的块。

但错误告诉您,您不能像这样使用|'a' | 'b'因为逻辑比较而没有意义。而是使用%in%

Source %in% c('htpWWW.BGDAILYNEWS.com', 'WWW.BGDAILYNEWS.COM')

我认为......如果我理解你在做什么,那么使用多个作业你会好得多:

County = vector(mode='character', length=length(Source))
County[County %in% c('htpWWW.BGDAILYNEWS.com', 'WWW.BGDAILYNEWS.COM')] <- 'Warren'
etc.

您还可以对此类事件使用switch语句:

myfun <- function(x) {
  switch(x,
         'httpWWW.BGDAILYNEWS.COM'='Warren',
         'httpWWW.HCLOCAL.COM'='Henry',
         etc...)
}

然后你想做一个简单的应用(sapply)将Source中的每个元素传递给myfun

County = sapply(Source, myfun)

或者最后,您可以使用factorslevels,但我会将其作为练习留给读者......

答案 1 :(得分:5)

另一种方法:

county <- c("Warren","Henry","Fayette","Christian")
sites <- c("WWW.BGDAILYNEWS.COM","WWW.HCLOCAL.COM","WWW.KENTUCKY.COM","WWW.KENTUCKYNEWERA.COM")
County <- county[match(gsub("^http","",Source), sites)]

对于与任何给定输入都不匹配的字符串,这将返回NA

使用哈德利的建议(lookup-tables-character-subsetting):

lookup <- c(WWW.BGDAILYNEWS.COM="Warren", WWW.HCLOCAL.COM="Henry", WWW.KENTUCKY.COM="Fayette", WWW.KENTUCKYNEWERA.COM="Christian")
County <- unname(lookup[gsub("^http","",Source)])