user6633625673888
user6633625673888

Reputation: 635

R Convert list to lowercase

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.

Upvotes: 28

Views: 69444

Answers (2)

MrFlick
MrFlick

Reputation: 206232

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.

Upvotes: 41

Josh Stevens
Josh Stevens

Reputation: 4221

Use gsub

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

this will remove all your / out of your variable.

Upvotes: 1

Related Questions