I'm setting up a data table & expected to transform all data to be in lower-case, thought it would look neat. How can I do that ?
Asked
Active
Viewed 105 times
2 Answers
3
We can use
library(data.table)
setDT(TestData)[, lapply(.SD, tolower)]
Or using tidyverse
library(tidyverse)
TestData %>%
mutate_if(is.character, tolower)

akrun
- 874,273
- 37
- 540
- 662
-
Thanks. Data table name is TestData. Is the code below is correct ? TestData[, lapply(.SD, tolower)] – Mr.KT Apr 17 '19 at 12:37
-
-
1I tried but throwing up an error Error in tolower(`Address Line 1`) : invalid input 'Dolní 3137/100' in 'utf8towcs' – Mr.KT Apr 17 '19 at 12:42
-
1@Mr.KT I think that is a encoding issue, you may have to check the settings – akrun Apr 17 '19 at 12:44
-
-
1
-
1
Here is an alternative that can also be combined with mutate
from dplyr
:
purrr::map(Filter(is.character,my_data),~tolower(.x))
We can also achieve the same using a combination of dplyr
and purrr
as suggested by
@Shinobi_Atobe
my_data %>%
keep(is.character) %>%
map(~tolower(.x))

NelsonGon
- 13,015
- 7
- 27
- 57
-
1probably just semantics, but I'd have gone with `my_data %>% keep(is.character) %>% map(~tolower(.x))` – Shinobi_Atobe Apr 17 '19 at 14:00
-
@Shinobi_Atobe I'll add that to reflect integration with `dplyr`. Thanks! – NelsonGon Apr 17 '19 at 14:01
-
-
1@Shinobi_Atobe Error in my_data %>% keep(is.character) %>% map(~tolower(.x)) : could not find function "%>%" – Mr.KT Apr 17 '19 at 15:46
-
@Mr.KT If you don't have these packages, please run `install.packages(c("purrr","dplyr"))`;`library(purrr)`;`library(dplyr)` first. – NelsonGon Apr 17 '19 at 15:47