Convert data.frame columns from factors to characters

前端 未结 18 1122
时光取名叫无心
时光取名叫无心 2020-11-22 04:43

I have a data frame. Let\'s call him bob:

> head(bob)
                 phenotype                         exclusion
GSM399350 3- 4- 8- 25- 44+         


        
相关标签:
18条回答
  • 2020-11-22 05:12

    New function "across" was introduced in dplyr version 1.0.0. The new function will supersede scoped variables (_if, _at, _all). Here's the official documentation

    library(dplyr)
    bob <- bob %>% 
           mutate(across(where(is.factor), as.character))
    
    0 讨论(0)
  • 2020-11-22 05:14

    If you would use data.table package for the operations on data.frame then the problem is not present.

    library(data.table)
    dt = data.table(col1 = c("a","b","c"), col2 = 1:3)
    sapply(dt, class)
    #       col1        col2 
    #"character"   "integer" 
    

    If you have a factor columns in you dataset already and you want to convert them to character you can do the following.

    library(data.table)
    dt = data.table(col1 = factor(c("a","b","c")), col2 = 1:3)
    sapply(dt, class)
    #     col1      col2 
    # "factor" "integer" 
    upd.cols = sapply(dt, is.factor)
    dt[, names(dt)[upd.cols] := lapply(.SD, as.character), .SDcols = upd.cols]
    sapply(dt, class)
    #       col1        col2 
    #"character"   "integer" 
    
    0 讨论(0)
  • Another way is to convert it using apply

    bob2 <- apply(bob,2,as.character)
    

    And a better one (the previous is of class 'matrix')

    bob2 <- as.data.frame(as.matrix(bob),stringsAsFactors=F)
    
    0 讨论(0)
  • 2020-11-22 05:18

    You should use convert in hablar which gives readable syntax compatible with tidyverse pipes:

    library(dplyr)
    library(hablar)
    
    df <- tibble(a = factor(c(1, 2, 3, 4)),
                 b = factor(c(5, 6, 7, 8)))
    
    df %>% convert(chr(a:b))
    

    which gives you:

      a     b    
      <chr> <chr>
    1 1     5    
    2 2     6    
    3 3     7    
    4 4     8   
    
    0 讨论(0)
  • 2020-11-22 05:20

    If you want a new data frame bobc where every factor vector in bobf is converted to a character vector, try this:

    bobc <- rapply(bobf, as.character, classes="factor", how="replace")
    

    If you then want to convert it back, you can create a logical vector of which columns are factors, and use that to selectively apply factor

    f <- sapply(bobf, class) == "factor"
    bobc[,f] <- lapply(bobc[,f], factor)
    
    0 讨论(0)
  • 2020-11-22 05:21

    Or you can try transform:

    newbob <- transform(bob, phenotype = as.character(phenotype))
    

    Just be sure to put every factor you'd like to convert to character.

    Or you can do something like this and kill all the pests with one blow:

    newbob_char <- as.data.frame(lapply(bob[sapply(bob, is.factor)], as.character), stringsAsFactors = FALSE)
    newbob_rest <- bob[!(sapply(bob, is.factor))]
    newbob <- cbind(newbob_char, newbob_rest)
    

    It's not good idea to shove the data in code like this, I could do the sapply part separately (actually, it's much easier to do it like that), but you get the point... I haven't checked the code, 'cause I'm not at home, so I hope it works! =)

    This approach, however, has a downside... you must reorganize columns afterwards, while with transform you can do whatever you like, but at cost of "pedestrian-style-code-writting"...

    So there... =)

    0 讨论(0)
提交回复
热议问题