How to convert all column data type to numeric and character dynamically?

前端 未结 5 1254
礼貌的吻别
礼貌的吻别 2020-12-12 00:34

I convert my columns data type manually:

data[,\'particles\'] <- as.numeric(as.character(data[,\'particles\']))

This not ideal as the da

相关标签:
5条回答
  • 2020-12-12 01:01

    If you don't know which columns need to be converted beforehand, you can extract that info from your dataframe as follows:

    vec <- sapply(dat, is.factor)
    

    which gives:

    > vec
    particles  humidity timestamp      date 
         TRUE      TRUE     FALSE     FALSE 
    

    You can then use this vector to do the conversion on the subset with lapply:

    # notation option one:
    dat[, vec] <- lapply(dat[, vec], function(x) as.numeric(as.character(x)))
    # notation option two:
    dat[vec] <- lapply(dat[vec], function(x) as.numeric(as.character(x)))
    

    If you want to detect both factor and character columns, you can use:

    sapply(dat, function(x) is.factor(x)|is.character(x))
    
    0 讨论(0)
  • 2020-12-12 01:07

    Use lapply:

    cols <- c("particles", "nox", ...)
    
    data[,cols] <- lapply(data[,cols], function(x) as.numeric(as.character(x)))
    
    0 讨论(0)
  • Another option using mutate_if() from dplyr which allows you to operate on columns for which a predicate returns TRUE

    library(dplyr)
    df %>% 
      mutate_if(is.factor, funs(as.numeric(as.character(.))))
    

    Note: This method will work for your follow up question as well

    0 讨论(0)
  • 2020-12-12 01:11

    We can use data.table

    library(data.table) 
    setDT(df)[, lapply(.SD, function(x) if(is.factor(x)) as.numeric(as.character(x)) else x)]
    
    0 讨论(0)
  • 2020-12-12 01:23

    The best option is I think apply

    You can do

    newD<-apply(data[,"names"], 2,function(x) as.numeric(as.character(x)))
    

    where in "names" you put all the variables you want. Then apply with 2 as second argument will apply the function(x) on all the columns(if you put 1 its by rows) of the first argument. And you can save it as new dataset or rewrite the old one with

    data[,"names"]<-apply....
    
    0 讨论(0)
提交回复
热议问题