How to Check If Each Element in a Vector Is Integer or Not in R

How to check if each element in a vector is integer or not in R?

The simplest (and fastest!) thing is probably this:

stopifnot( all(y == floor(y)) )

...So trying it out:

y <- c(3,4,9)
stopifnot( all(y == floor(y)) ) # OK

y <- c(3,4.01,9)
stopifnot( all(y == floor(y)) ) # ERROR!

If you want a better error message:

y <- c(3, 9, NaN)
if (!isTRUE(all(y == floor(y)))) stop("'y' must only contain integer values")

Test if a vector contains a given element

Both the match() (returns the first appearance) and %in% (returns a Boolean) functions are designed for this.

v <- c('a','b','c','e')

'b' %in% v
## returns TRUE

match('b',v)
## returns the first location of 'b', in this case: 2

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

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

Check if the number is integer

Another alternative is to check the fractional part:

x%%1==0

or, if you want to check within a certain tolerance:

min(abs(c(x%%1, x%%1-1))) < tol

R: fastest way to check presence of each element of a vector in each of the columns of a matrix

Rcpp is awesome for problems like this. It is quite possible that there is some way to do it with data.table or with an existing function, but with the inline package it takes almost less time to write it yourself than to find out.

require(inline)

ispresent.cpp <- cxxfunction(signature(a="integer", B="integer"),
plugin="Rcpp", body='
IntegerVector av(a);
IntegerMatrix Bm(B);
int i,j,k;
LogicalMatrix out(av.size(), Bm.ncol());
for(i = 0; i < av.size(); i++){
for(j = 0; j < Bm.ncol(); j++){
for(k = 0; k < Bm.nrow() && av[i] != Bm(k, j); k++);
if(k < Bm.nrow()) out(i, j) = true;
}
}
return(out);
')

set.seed(123)
a1 <- a(1000)
B1 <- B(20000)
system.time(res.cpp <- ispresent.cpp(a1, B1))
   user  system elapsed 
0.442 0.005 0.446
res1 <- ispresent1(a1,B1)
identical(res1, res.cpp)
[1] TRUE

how to determine if a character vector is a valid numeric or integer vector

As discussed here, checking if as.numeric returns NA values is a simple approach to checking if a character string contains numeric data. Now you can do something like:

myDF2 <- lapply(myDF, function(col) {
if (suppressWarnings(all(!is.na(as.numeric(as.character(col)))))) {
as.numeric(as.character(col))
} else {
col
}
})
str(myDF2)
# List of 3
# $ w : num [1:2] 1 2
# $ x.y: num [1:2] 0.1 0.2
# $ x.z: Factor w/ 2 levels "cat","dog": 1 2

Check whether an element in a character vector can be converted to numeric in R

Perhaps, you can use regex to find if all the values in a column are either an integer or float.

can_convert_to_numeric <- function(x) {
all(grepl('^(?=.)([+-]?([0-9]*)(\\.([0-9]+))?)$', x, perl = TRUE))
}

sapply(df[catCols], can_convert_to_numeric)
# cat1 cat2
#FALSE TRUE

Alternatively, to get values that cannot be converted to numeric we can use grep as :

values_which_cannot_be_numeric <- function(x) {
grep('^(?=.)([+-]?([0-9]*)(\\.([0-9]+))?)$', x, perl = TRUE, invert = TRUE, value = TRUE)
}

lapply(df[catCols], values_which_cannot_be_numeric)

#$cat1
#[1] "some_string"

#$cat2
#character(0)

Regex taken from here.


If you use type.convert you don't have to worry about this at all.

df <- type.convert(df, as.is = TRUE)
str(df)

#'data.frame': 4 obs. of 3 variables:
# $ cat1 : chr "1.12354" "1.4548" "1.9856" "some_string"
# $ cat2 : num 1.46 1.15 1.96 1.32
# $ target: int 0 1 1 0

Filtering a numeric vector based on whether the number is an integer or not

You may try using floor

integerFunction <- function(x) {
x - floor(x) == 0
}
integerFunction(c(1.0, 5.0, 5.3, 10.0, 2.5))

[1] TRUE TRUE FALSE TRUE FALSE

Check the type of a vector variable in R

In R integer or character or double don't really exist. Everything (or to be more correct, every atomic data) is a vector. Thus if a = 1L, then a is not an integer but a vector of integers with a length of 1.

Both tests return character. You can test the length of the vector.

You can read Data Structures, a chapter of "Adavanced R" to learn more.



Related Topics



Leave a reply



Submit