fcas80
September 27, 2022, 6:09pm
1
Hi. tolower works fine on a vector, but produces strange results (but not an error) on a dataframe. What is it doing with the dataframe?
x = c("Bug","Cat","dOG","emU","FroG")
tolower(x)
y <- c("a","B","C","d","E")
df <- data.frame(x, y)
tolower(df)
[1] "bug" "cat" "dog" "emu" "frog"
[1] "c("bug", "cat", "dog", "emu", "frog")"
[2] "c("a", "b", "c", "d", "e")"
The last two lines above are not displaying " c ( slash "a slash " etc.
Using across() within the mutate() function will ensure that you apply tolower to each column of the df individually
require(dplyr)
x = c("Bug","Cat","dOG","emU","FroG")
y <- c("a","B","C","d","E")
df <- data.frame(x, y)
df %>%
mutate(., across(.cols = everything(), tolower))
fcas80
September 28, 2022, 2:11am
3
cmeuli07,
Thank you for your solution.
But why does tolower applied to a dataframe produce such strange results?
Because, according to the documentation, it can only be applied to character vectors or such objects which can be coerced to character. Hence, using tolower()
on a data.frame
coerces the data.frame
into a chr
vector first and then does the lowering of letters. But since there is no way back, you end up with a coerced object which has been transformed to lower letters only.
2 Likes
because toLower will attempt to convert anything that is not a character to a character and go from there
as.character(df)
going further than that, I understand there to be a relationship to the behaviour of
c(df)
i.e. the stripping of the data.frame class from the underlying list of the contents of the data.frame
system
Closed
October 5, 2022, 8:40am
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.