Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Convert list to lowercase

Tags:

Var1 is a list:

var1 <- list(c("Parts of a Day", "Time in Astronomy", "Star"),  c("Tree Tall", "Pine Tree")) 

How to convert all the characters to lowercase? The desired answer is the following list:

var1 <- list(c("parts of a day", "time in astronomy", "star"),  c("tree tall", "pine tree")) 

I used

as.list(tolower(var1)) 

But it gives the following answer with unwanted \

[[1]] [1] "c(\"parts of a day\", \"time in astronomy\", \"star\")"  [[2]] [1] "c(\"tree tall\", \"pine tree\")" 

Thanks.

like image 731
user6633625673888 Avatar asked May 24 '15 22:05

user6633625673888


People also ask

How do I convert all caps to lowercase in R?

tolower() method in R programming is used to convert the uppercase letters of string to lowercase string. Return: Returns the lowercase string.

How do you make all variables in lowercase in R?

In R, the easiest way to convert column names to lowercase is by using the functions names() and tolower() . First, the names() function reads the column names of a data frame and returns them in a character vector. Next, the tolower() function transforms all characters of this vector to lowercase.

How do I change the case letter in R?

To convert a lowercase string to an uppercase string in R, use the toupper() method. The toupper() method changes the case of a string to the upper. The toupper() function takes a string as an argument and returns the uppercase version of a string.

How do you capitalize all letters in R?

toupper() method in R programming is used to convert the lowercase string to uppercase string. Return: Returns the uppercase string.


2 Answers

You should use lapply to lower case each character vector in your list

lapply(var1, tolower)  # [[1]] # [1] "parts of a day"    "time in astronomy" "star"              #  # [[2]] # [1] "tree tall" "pine tree" 

otherwise tolower does as.character() on your entire list which is not what you want.

like image 107
MrFlick Avatar answered Sep 29 '22 05:09

MrFlick


Use gsub

gsub("/", "", var1) as.list(tolower(var1)) 

this will remove all your / out of your variable.

like image 37
Josh Stevens Avatar answered Sep 29 '22 05:09

Josh Stevens