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?
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.
If you want to change column names only, you can try below syntax.
colnames(df) <- tolower(colnames(df))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With