Convert data.frame columns from factors to characters

前端 未结 18 1448
时光取名叫无心
时光取名叫无心 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: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)
    

提交回复
热议问题