Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lower case for a (factor) data frame column

Tags:

r

I have a data.frame in this format:

'data.frame':   244 obs. of  1 variable:
 $ names: Factor w/ 244 levels "ERA","BAKE",..: 1 2 3 4 5 6 7 8 9 10 ...

I used this to convert it to lower case:

df$names <- tolower(df$names)

and as result I get this:

> str(df)
'data.frame':   244 obs. of  1 variable:
 $ names: chr  "era" "bake" "and" "stock price" ...

How can I keep the structure of the initial data.frame while also converting to lower case?

like image 379
Eracog Avatar asked Mar 13 '16 19:03

Eracog


2 Answers

Look at the source of tolower (you can do this by just entering the variable name tolower in the console, or by entering print(tolower)):

if (!is.character(x)) 
    x <- as.character(x)

Your factor column is being forced to a character vector.

Instead, I believe you want:

levels(df$names) <- tolower(levels(df$names))

This is also more efficient, since we only have to replace the values of length(levels(df$names)) in memory, typically much smaller than replacing the full vector of nrow(df) values.

like image 171
MichaelChirico Avatar answered Oct 22 '22 23:10

MichaelChirico


If you want to change column names only, you can try below syntax.

colnames(df) <- tolower(colnames(df))
like image 27
Saranga Avatar answered Oct 22 '22 22:10

Saranga