0

I have a R code to filter the data frame values - age and no. of cars. I want to remove the floating values in car column if there are any. For example:

Age Cars
29  2
27  3
18  2.3
31  4
17  2
20  -1

Here I want to remove the records whose age is <18, cars >=0 and also records having 2.3 cars (floating) value in R.

bikers_inconsistent <- function(n){
  # To check age of the rider >=18 and no. of cars >=0 
  x <- n[n$age>=18 & n$cars>=0 ,]

 return(x)
}

I have written the code to retrieve cars >=0 and age>=18. But struggling to filter the data which have floating values.

Please help as I'm new to R

joran
  • 169,992
  • 32
  • 429
  • 468

2 Answers2

1

Floating values can be tough due to precision issues. I would recommend using a tolerance for how far from an integer a number can be while still counting as an integer. Have a look at this important FAQ for why a tolerance is needed. Here's a simple implementation:

is_float = function(x, tolerance = 1e-6) {
  abs(x - round(x)) > tolerance
}


bikers_inconsistent <- function(n){
  # To check age of the rider >=18 and no. of cars >=0 
  x <- n[n$age>=18 & n$cars>=0 & !is_float(n$cars),]
  
  return(x)
}

bikers_inconsistent(data)
#   age cars
# 1  29    2
# 2  27    3
# 4  31    4
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
0

You can check if a number is an integer by using the modulo operation (%%): If a number modulo 1 is 0, that number is an integer.

data <- data.frame(
  age = c(29, 27, 18, 31, 17, 20),
  cars = c(2, 3, 2.3, 4, 2, -1)
)
bikers_inconsistent <- function(n){
  # To check age of the rider >=18 and no. of cars >=0 
  x <- n[n$age>=18 & n$cars>=0 & n$cars %% 1 == 0,]
  
  return(x)
}
bikers_inconsistent(data)

#  age cars
#1  29    2
#2  27    3
#4  31    4

Another option would be to check if n$cars == as.integer(n$cars).

NiklasvMoers
  • 309
  • 2
  • 13