How to compare boolean vectors in R - r

I have a vector v and I want to find all those elements, that have values between 4 and 7.
v = c(1:9)
# indices of elements with values larger than 4
which(v > 4)
# indices of elements with values smaller than 7
which(v < 7)
v>4 and v<7 give boolean vectors, which I'd like to combine. I tried the following, which did not work for me,...
# combination?
matching = which(v>4 && v<7) # does not work
How can I applay a boolean operation on two boolean vectors, that gives me a resulting vector?

Use & and not &&. R is different from other languages in that the & is not a bitwise and, but a logical operator.
&& only evaluates the first element of each vector:
‘&’ and ‘&&’ indicate logical AND and ‘|’ and ‘||’ indicate
logical OR. The shorter form performs elementwise comparisons in
much the same way as arithmetic operators. The longer form
evaluates left to right examining only the first element of each
vector. Evaluation proceeds only until the result is determined.
The longer form is appropriate for programming control-flow and
typically preferred in ‘if’ clauses.
See ?"&&" for more details.

Related

vector - character/integer class (under the hood)

Starting to learn R, and I would appreciate some help understanding how R decides the class of different vectors. I initialize vec <- c(1:6) and when I perform class(vec) I get 'integer'. Why is it not 'numeric', because I thought integers in R looked like this: 4L
Also with vec2 <- c(1,'a',2,TRUE), why is class(vec2) 'character'? I'm guessing R picks up on the characters and automatically assigns everything else to be characters...so then it actually looks like c('1','a','2','TRUE') am I correct?
Type the following, you can see the help page of the colon operator.
?`:`
Here is one paragraph.
For numeric arguments, a numeric vector. This will be of type integer
if from is integer-valued and the result is representable in the R
integer type, otherwise of type "double" (aka mode "numeric").
So, in your example c(1:6), since 1 for the from argument can be representable in R as integer, the resulting sequence becomes integer.
By the way, c is not needed to create a vector in this case.
For the second question, since in a vector all the elements have to be in the same type, R will automatically convert all the elements to the same. In this case, it is possible to convert everything to be character, but it is not possible to convert "a" to be numeric, so it results in a character vector.

r programming - check for every value in a vector if it is numeric

I am trying to create a logical vector in R, which will indicate for every value of a complete vector, if it is numeric or not.
I am trying to use the function is.numeric but it will only check if all the vector is numeric or not like that:
vec<-c(1,2,3,"lol")
t<-is.numeric(c[])
t
will produce FALSE
i looked here, but it will only tell how to check the entire vector and get a single value
i looked here, but the issue is not finite vs infinite
i am trying to take a data set, with some values being numbers and other being a string that implies that there is no value, and find a minimum only in the numeric values. for that i try to create a logical vector that will say for every entry of the vector if it is numeric or not. this is important for me to create that vector and i am trying to avoid a complete loop and construction of that vector if possible.
We can use numeric coercion to our advantage. R will message us to be sure that we meant to change the strings to NA. In this case, it is exactly what we are looking for:
!is.na(as.numeric(vec))
#[1] TRUE TRUE TRUE FALSE
#Warning message:
#NAs introduced by coercion
We can use grepl to get a logical vector. We match that includes only numbers from start (^) to end ($). I also included the possibility that there could be negative and floating point numbers.
grepl('^-?[0-9.]+$', vec)
#[1] TRUE TRUE TRUE FALSE
NOTE: There will be no warning messages.

Different types in R Vector

I am currently reading R Cookbook. A statement made in the book says the following:
"A vector can contain etiher numbers, strings, or logical but not mixture."
I wanted to test this out so I created the following vector:
u <- c("c",1,TRUE)
u
[1] "c" "1" "TRUE"
This looks like it changed everything to a string. I then did the following:
u <- c(TRUE,0)
u
[1] 1 0
So it looks like when there is a mixture of types, they are all converted to a similar type. My question is how does R determine which type?
As described in this article, the bigger type wins. That is, the type that can represent more wins.
In your first example, character can represent all of the other three, so it coerces everyone to character.
The second case the integer is bigger than the logical.
This answer lists the full hierarchy.
raw < logical < integer < double < complex < character < list
You might find the Coercion section of the Advanced R book helpful. The elements are coerced from least to most flexible type (logical > integer > double > character).

Ifelse function R

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?
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.

Why does 1..99,999 == "1".."99,999" in R, but 100,000 != "100,000"?

In the console, go ahead and try
> sum(sapply(1:99999, function(x) { x != as.character(x) }))
0
For all of values 1 through 99999, "1" == 1, "2" == 2, ..., 99999 == "99999" are TRUE. However,
> 100000 == "100000"
FALSE
Why does R have this quirky behavior, and is this a bug? What would be a workaround to, e.g., check if every element in an atomic character vector is in fact numeric? Right now I was trying to check whether x == as.numeric(x) for each x, but that fails on certain datasets due to the above problem!
Have a look at as.character(100000). Its value is not equal to "100000" (have a look for yourself), and R is essentially just telling you so.
as.character(100000)
# [1] "1e+05"
Here, from ?Comparison, are R's rules for applying relational operators to values of different types:
If the two arguments are atomic vectors of different types, one is
coerced to the type of the other, the (decreasing) order of
precedence being character, complex, numeric, integer, logical and
raw.
Those rules mean that when you test whether 1=="1", say, R first converts the numeric value on the LHS to a character string, and then tests for equality of the character strings on the LHS and RHS. In some cases those will be equal, but in other cases they will not. Which cases produce inequality will be dependent on the current settings of options("scipen") and options("digits")
So, when you type 100000=="100000", it is as if you were actually performing the following test. (Note that internally, R may well/probably does use something different than as.character() to perform the conversion):
as.character(100000)=="100000"
# [1] FALSE

Resources