Reputation: 595
I am trying execute this code but getting result NA.
> node1<- paste0("train$", rule, collapse=" & ")
> node1
[1] "train$feat_11< 5.477 & train$feat_60< 4.687"
>x<-ifelse(node1,1,0)
[1] NA
How can I use character vector in if else function?
Upvotes: 1
Views: 364
Reputation: 4537
Logical vectors and character vectors are two very different things in R.
class(node1)
#>[1] "character"
You must first parse and evaluate the string.
lNode1 = eval(parse(text=node1))
class(lNode1)
#>[1] "logical"
x<-ifelse(lNode1,1,0)
#>a list of 1's and 0's
That being said, however, your ifelse
statement is redundant. A logical vector will coerce to an integer vector when used in a fashion that requires it. For example, you can sum(lNode1)
and get the number of times you pass both rules.
Upvotes: 3