2

There is a nice discussion of how to convert character into numerics in this SO here. Maybe I missed something in that post, but what would one do if one does not know which columns are "convertable" (if any) ? Is it possible to check for convertability ? In addition, I usually suppress factor conversion (like character better) - so characters should be characters (not factors).

df <- data.frame(a=as.character(c(NA, 1/3)), b=letters[1:2], c=c('1|2', '4|2'), d=as.character(3:4), stringsAsFactors = F)

Then apply ... some function f ... to get:

str(f(df))
'data.frame':   2 obs. of  4 variables:
 $ a: num  NA 0.333
 $ b: chr  "a" "b"
 $ c: chr  "1|2" "4|2"
 $ d: int  3 4

How to achieve this for any data.frame not known beforehand ?

user3375672
  • 3,548
  • 8
  • 37
  • 66

1 Answers1

2

You could do something like this (not very elegant though).

fun1 <- function(i) {
  if (!all(is.na(as.numeric(df[, i])))){
    as.numeric(df[, i])
  } else {
    df[, i]
  }
}

df1 <- "names<-"(cbind.data.frame(lapply(seq_along(df), fun1),
                                  stringsAsFactors=FALSE), names(df))

> str(df1)
'data.frame':   2 obs. of  4 variables:
 $ a: num  NA 0.333
 $ b: chr  "a" "b"
 $ c: chr  "1|2" "4|2"
 $ d: num  3 4

Or more generally:

convertiblesToNumeric <- function(x){
  x2 <- cbind.data.frame(lapply(seq_along(x), function(i) {
    if (!all(is.na(as.numeric(x[, i])))){
      as.numeric(x[, i])
      } else {
        x[, i]
        }
    }), stringsAsFactors=FALSE)
  names(x2) <- names(x)
  return(x2)
}

df1 <- convertiblesToNumeric(df)
> str(df1)
'data.frame':   2 obs. of  4 variables:
 $ a: num  NA 0.333
 $ b: chr  "a" "b"
 $ c: chr  "1|2" "4|2"
 $ d: num  3 4
jay.sf
  • 46,523
  • 6
  • 46
  • 87