Lower case for data frame column

I have df in these formats:

'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 use lower case:

 df$names <- tolower(df$names) 

and as a result of the format I take this:

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

Why can't I keep the structure of the original df after using lowercase?

+5
source share
2 answers

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

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

Your factor column is forcibly bound to the character vector.

Instead, I believe you want:

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

It is also more efficient, since we only need to replace the length(levels(df$names)) values ​​in memory, as a rule, much less than replacing the full vector of nrow(df) values.

+14
source

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

 colnames(df) <- tolower(colnames(df)) 
0
source

Source: https://habr.com/ru/post/1244969/


All Articles