Answers for "r - check if a column has non numrical values"

0

r - check if a column has non numrical values

df <- data.frame(list(A=c(1, 2, 3, 4, 5, 6, 7, 8, 9), B=c("40g", "< 2", "thx", "about 1", "1-2", "1/2", 3, 2.3, "two")))
df$B <- as.character(df$B)

myscan <- function(x) {
 new <- vector("numeric",length(x))
 for(i in seq_along(x)) {
   new[i] <- readline(sprintf("Non numeric entry '%s' new value to set: ",x[i]))
 }
 as.numeric(new)
}

# get the entries 
notNum <- is.na( as.numeric(df$B) )
# Loop and ask for updates
df$B[notNum] <-  myscan(df$B[notNum])
Posted by: Guest on May-25-2021

Code answers related to "r - check if a column has non numrical values"

Browse Popular Code Answers by Language