Home > Back-end >  How to cast only eligible columns
How to cast only eligible columns

Time:04-14

If I have hundreds of character columns in a dataframe, but some of them have values who can be converted in numeric, since they are all integer or float, and other can't, since some of them are actually character. How can I cast only the eligible columns into numeric?

CodePudding user response:

One approach is to first define a function to cast a single vector into numbers if it can be turned to numeric without generating NAs, then lapply this to the dataframe.

x = c("a", "3", "")
y = c("3", "3", "3")
z = c("2", NA, "3")
df = data.frame(x,y,z)

castIfNumeric <- function(x) if( all(!is.na(as.numeric(na.omit(x)))) ) as.numeric(x) else x
df <- as.data.frame(lapply(df, castIfNumeric))
str(df)

'data.frame':   3 obs. of  3 variables:
 $ x: chr  "a" "3" ""
 $ y: num  3 3 3
 $ z: num  2 NA 3
  • Related