R: ifelse on string
Solution 1
I suggest you break down that deeply nested ifelse
statement into more manageable chunks.
But the error is telling you that you cannot use |
like that. 'a' | 'b'
doesn't make sense since its a logical comparison. Instead use %in%
:
Source %in% c('htpWWW.BGDAILYNEWS.com', 'WWW.BGDAILYNEWS.COM')
I think... If I understand what you're doing, you will be much better off using multiple assignments:
County = vector(mode='character', length=length(Source))
County[County %in% c('htpWWW.BGDAILYNEWS.com', 'WWW.BGDAILYNEWS.COM')] <- 'Warren'
etc.
You can also use a switch
statement for this type of thing:
myfun <- function(x) {
switch(x,
'httpWWW.BGDAILYNEWS.COM'='Warren',
'httpWWW.HCLOCAL.COM'='Henry',
etc...)
}
Then you want to do a simple apply (sapply
) passing each element in Source
to myfun
:
County = sapply(Source, myfun)
Or finally, you can use factors
and levels
, but I'll leave that as an exercise to the reader...
Solution 2
A different approach:
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)]
This will return NA
for strings that do no match any of the given inputs.
Using Hadley's suggestion (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)])
NiuBiBang
Updated on June 04, 2022Comments
-
NiuBiBang almost 2 years
I am populating a new variable of a dataframe, based on string conditions from another variable. I receive the following error msg:
Error in Source == "httpWWW.BGDAILYNEWS.COM" | Source == : operations are possible only for numeric, logical or complex types
My code is as follows:
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') )))
-
Señor O almost 11 years+1 for
switch
. If you find yourself using more than 2ifelse
's, you should probably be usingswitch
orcut
. -
hadley almost 11 yearsYou could also eliminate the
match
and use character subsetting directly: github.com/hadley/devtools/wiki/… -
NiuBiBang almost 11 years@Justin, sorry to ask but can you post a more specific example for the
switch
function? Specifically, how do I create theCounty
variable from theSource
variable usingswitch
? Much agreed that myifelse
s are not parsimonious.