How to convert dataframe columns from factors to characters

+1 vote

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 Sahiti
• 6,370 points
19,542 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
• 420 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
8,673 views
0 votes
1 answer

How to extract specific columns from a dataframe in R?

Hi@akhtar, You can use the select method to ...READ MORE

answered Aug 7, 2020 in Data Analytics by MD
• 95,460 points
1,958 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 14, 2018 in Data Analytics by Sahiti
• 6,370 points
7,046 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 19, 2018 in Data Analytics by Abhi
• 3,720 points
585 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 19, 2018 in Data Analytics by Abhi
• 3,720 points
4,377 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
1,669 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 5, 2018 in Data Analytics by Kalgi
• 52,350 points
1,110 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
1,928 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP