How to check if all values in a vector are integer or not in R?

R ProgrammingServer Side ProgrammingProgramming

To check whether all values in a vector in R are integer or not, we can round the vector using floor function then subtract the vector values from it and check whether the output is zero or not. If the output will be zero that means the value is integer otherwise it is not. The floor function returns the largest integer that is smaller or equal to the actual value. For example, if we have a vector x then it can be done as x-floor(x)==0.

Example1

Live Demo

> x1<-sample(0:10,200,replace=TRUE)
> x1

Output

[1] 4 0 2 8 6 1 3 7 3 4 0 7 2 10 9 7 9 8 4 5 5 7 8 9 6
[26] 1 1 10 2 6 3 10 8 2 1 1 2 5 4 0 1 0 10 7 0 1 3 5 7 5
[51] 4 10 0 0 8 9 7 2 7 2 0 0 0 2 4 2 6 9 9 9 1 0 4 10 7
[76] 7 0 10 8 9 2 3 0 1 3 6 1 2 10 0 2 7 1 10 5 8 1 7 9 9
[101] 2 9 5 3 0 4 7 10 4 3 9 10 5 4 0 2 4 7 5 7 10 3 9 5 1
[126] 6 4 8 0 6 7 1 2 10 2 7 1 1 9 6 3 2 5 9 6 3 5 4 1 10
[151] 0 9 1 0 6 3 4 2 3 2 3 3 1 3 7 5 4 0 6 10 4 3 9 6 10
[176] 2 8 7 10 2 9 2 8 5 2 5 2 3 3 7 8 3 3 5 3 2 10 3 6 2

Example

> x1-floor(x1)==0

Output

[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[76] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[91] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[106] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[121] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[136] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[151] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[166] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[181] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[196] TRUE TRUE TRUE TRUE TRUE

Example2

Live Demo

> x2<-sample(c(rnorm(10),1,5),100,replace=TRUE)
> x2

Output

[1] 1.00000000 -0.06451323 -1.30754384 -1.40935749 1.00000000 -1.30754384
[7] 1.00000000 1.03956333 -0.29699121 5.00000000 -1.25592848 -1.40935749
[13] -1.40935749 -1.40935749 -1.30754384 0.28637866 -1.40935749 -1.25592848
[19] -0.29699121 -0.95734141 -1.25592848 0.28637866 -0.95734141 -0.29699121
[25] -1.25592848 -0.61126941 -1.30754384 -0.95734141 -0.29699121 -0.61126941
[31] -0.08366718 -0.95734141 -0.06451323 -1.25592848 1.00000000 -0.06451323
[37] -0.08366718 5.00000000 5.00000000 -1.25592848 0.28637866 -0.95734141
[43] -0.95734141 -1.25592848 -0.95734141 1.00000000 -0.61126941 1.03956333
[49] -1.25592848 -0.29699121 1.03956333 -1.40935749 -0.29699121 5.00000000
[55] -1.40935749 0.28637866 -1.25592848 5.00000000 -0.95734141 -1.40935749
[61] 5.00000000 -1.30754384 -0.61126941 -0.08366718 5.00000000 -0.06451323
[67] 5.00000000 -0.95734141 -1.40935749 -0.95734141 5.00000000 -0.08366718
[73] -1.30754384 -0.06451323 1.03956333 1.03956333 -1.30754384 -0.61126941
[79] -1.40935749 -0.08366718 -0.61126941 -0.61126941 1.00000000 -0.29699121
[85] -1.40935749 -0.29699121 -1.30754384 -0.61126941 5.00000000 5.00000000
[91] -0.06451323 -0.29699121 1.00000000 5.00000000 -0.06451323 -0.08366718
[97] -0.95734141 5.00000000 -0.29699121 1.00000000

Example

> x2-floor(x2)==0

Output

[1] TRUE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
[37] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
[49] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE
[61] TRUE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
[73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
[85] FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE
[97] FALSE TRUE FALSE TRUE

Example3

Live Demo

> x3<-sample(c(rexp(10,1.25),0:1),100,replace=TRUE)
> x3

Output

[1] 0.05460675 4.38033999 0.05460675 0.37369945 1.96152318 1.25238146
[7] 1.05630048 1.01599745 4.38033999 1.25238146 1.00000000 0.65649865
[13] 4.38033999 4.38033999 1.25238146 4.38033999 1.25238146 1.00000000
[19] 1.25238146 1.75622583 1.01599745 4.38033999 0.65649865 1.05630048
[25] 1.21278164 1.01599745 1.05630048 1.75622583 1.00000000 1.75622583
[31] 1.75622583 1.96152318 0.00000000 1.96152318 1.21278164 1.01599745
[37] 0.65649865 1.25238146 0.37369945 1.21278164 1.00000000 1.21278164
[43] 0.05460675 1.75622583 0.37369945 1.75622583 0.65649865 0.05460675
[49] 1.75622583 0.37369945 1.75622583 0.00000000 1.05630048 1.96152318
[55] 1.21278164 1.05630048 1.05630048 1.05630048 1.96152318 1.05630048
[61] 0.05460675 1.05630048 0.00000000 0.65649865 0.00000000 1.96152318
[67] 4.38033999 1.05630048 1.96152318 1.25238146 4.38033999 4.38033999
[73] 1.05630048 0.05460675 4.38033999 1.21278164 1.25238146 1.75622583
[79] 1.00000000 1.05630048 1.21278164 1.25238146 1.05630048 1.96152318
[85] 0.05460675 4.38033999 1.05630048 0.65649865 0.05460675 1.05630048
[91] 1.21278164 0.00000000 1.05630048 0.00000000 4.38033999 0.65649865
[97] 0.05460675 0.65649865 1.25238146 1.00000000

Example

> x3-floor(x3)==0

Output

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
[13] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
[37] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[49] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[61] FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[73] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
[85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE
[97] FALSE FALSE FALSE TRUE

Example4

Live Demo

> x4<-sample(c(runif(10,1,5),1:10),100,replace=TRUE)
> x4

Output

[1] 3.225631 8.000000 8.000000 2.291716 4.000000 2.666931 3.523014
[8] 2.861269 3.248689 1.000000 1.000000 3.523014 5.000000 2.784298
[15] 2.861269 5.000000 2.861269 3.225631 2.303123 6.000000 3.310928
[22] 2.291716 1.000000 2.000000 5.000000 3.225631 2.666931 2.000000
[29] 2.303123 2.303123 2.303123 3.523014 4.000000 3.000000 2.291716
[36] 1.690408 3.310928 3.248689 10.000000 2.784298 1.000000 2.784298
[43] 3.310928 6.000000 2.291716 7.000000 3.225631 2.303123 2.784298
[50] 2.291716 1.000000 4.000000 2.861269 2.861269 8.000000 4.000000
[57] 1.000000 2.784298 3.523014 2.303123 2.303123 6.000000 2.861269
[64] 10.000000 10.000000 2.784298 3.225631 9.000000 3.225631 2.666931
[71] 3.000000 2.666931 3.310928 3.000000 5.000000 3.000000 2.666931
[78] 2.784298 3.225631 5.000000 8.000000 3.000000 4.000000 3.248689
[85] 9.000000 6.000000 2.303123 9.000000 3.000000 3.248689 2.303123
[92] 3.000000 1.690408 6.000000 2.303123 7.000000 2.861269 3.310928
[99] 3.000000 4.000000

Example

> x4-floor(x4)==0

Output

[1] FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
[13] TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
[25] TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
[37] FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE
[49] FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE
[61] FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
[73] FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE
[85] TRUE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE
[97] FALSE FALSE TRUE TRUE

Example5

Live Demo

> x5<-sample(c(round(runif(10,2,5),2),6:10),100,replace=TRUE)
> x5

Output

[1] 3.48 2.29 3.61 10.00 3.28 3.48 9.00 3.28 9.00 6.00 2.49 2.30
[13] 9.00 2.29 3.48 3.94 2.49 3.94 3.83 7.00 3.94 7.00 3.48 4.26
[25] 3.28 3.94 3.83 3.83 6.00 3.94 3.94 2.30 6.00 7.00 3.28 10.00
[37] 3.48 2.30 4.26 3.48 2.49 2.30 2.30 7.00 3.28 4.26 2.29 3.48
[49] 4.94 3.48 3.83 9.00 2.30 6.00 8.00 3.61 3.83 9.00 3.28 3.94
[61] 3.28 2.29 6.00 8.00 2.30 8.00 3.48 4.26 8.00 2.49 9.00 10.00
[73] 4.26 3.94 7.00 4.94 3.61 2.30 3.48 3.48 8.00 8.00 10.00 4.26
[85] 3.28 4.94 10.00 4.94 3.61 3.61 2.29 9.00 2.29 4.26 3.94 3.28
[97] 3.94 3.61 4.94 4.94

Example

> x5-floor(x5)==0

Output

[1] FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE
[13] TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE
[25] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE
[37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
[49] FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE
[61] FALSE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE
[73] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE
[85] FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
[97] FALSE FALSE FALSE FALSE
raja
Published on 05-Jan-2021 06:03:06
Advertisements