I have a question regarding selecting specific values from a vector in R. More specifically, I want to select all integer values from a given variable in my dataset (I want to use these to subset my data). Here is an example:
x <- seq(0,10,1/3)
Now I want to select all the observations in the vector x with integer numbers. My first idea was to use the is.integer
command, but this does not work. I found a workaround solution using the following:
> x==as.integer(x)
[1] TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
FALSE TRUE FALSE FALSE TRUE
[17] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
FALSE TRUE FALSE FALSE TRUE
Now I can simply type
> which(x==as.integer(x))
[1] 1 4 7 10 13 16 19 22 25 28 31
and I get the expected result (and I can use this vector for subsetting my dataset). But isn't there a more direct way to select integer values?