r - check if a column has non numrical values code example
Example: 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])