How to convert dataframe columns from factors to characters?

0 votes

I have considered a data frame named 'zz'.

head(zz)
                 phenotype                         exclusion
GSM399350 3- 4- 8- 25- 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-
GSM399351 3- 4- 8- 25- 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-
GSM399352 3- 4- 8- 25- 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-
GSM399353 3- 4- 8- 25+ 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-
GSM399354 3- 4- 8- 25+ 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-
GSM399355 3- 4- 8- 25+ 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-

The following command shows the dataframe has factors

class(zz$phenotype)
[1] "factor"

Now to convert I tried the following command

 as.character(head(zz))
[1] "c(3, 3, 3, 6, 6, 6)"       "c(3, 3, 3, 3, 3, 3)"      
[3] "c(29, 29, 29, 30, 30, 30)"

 I guess these are indices into the levels of the factors of the columns. This is not my desired result.

So any suggestions that can help me?

Apr 13, 2018 in Data Analytics by darklord
• 6,170 points
1,476 views

1 answer to this question.

0 votes

To replace all the variables to character class you can use the apply statement. Refer below code:

zz <- data.frame(lapply(zz, as.character), stringsAsFactors=FALSE)

You can use the following code to convert only the factor variables to character

i <- sapply(zz, is.factor)
zz[i] <- lapply(zz[i], as.character)

You can also use the dplyr package

library(dplyr)
zz %>% mutate_if(is.factor, as.character) -> 

 Another alternative is to use the purrr package in RStudio:

library(purrr)
library(dplyr)
zz %>% map_if(is.factor, as.character) %>% as_data_frame -> zz
answered Apr 13, 2018 by BHARANI
• 410 points

Related Questions In Data Analytics

0 votes
1 answer

How to extract specific columns from a DataFrame ?

Yes there are so many ways: df[,c(V2,V4)] and another ...READ MORE

answered Apr 11, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,277 views
0 votes
1 answer
+1 vote
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,170 points
189 views
0 votes
1 answer

How can I convert a list to a dataframe?

L = ['1', '2','3','4','5'] #create new df df ...READ MORE

answered Sep 18, 2018 in Data Analytics by Anmol
• 3,620 points
37 views
0 votes
1 answer

How do I convert a dataframe to a list?

For doing this first you'll have to ...READ MORE

answered Sep 18, 2018 in Data Analytics by Anmol
• 3,620 points
42 views
0 votes
1 answer

How to sort substring from a dataframe

First, extract the substring from the data ...READ MORE

answered Nov 4 in Data Analytics by Cherukuri
• 31,840 points
33 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
128 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 42,330 points
60 views
0 votes
1 answer
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
158 views