R将列表转换为小写

use*_*888 21 r list lowercase

Var1是一个列表:

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

如何将所有字符转换为小写?所需答案如下:

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

我用了

as.list(tolower(var1))
Run Code Online (Sandbox Code Playgroud)

但它给出了以下不必要的答案

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

[[2]]
[1] "c(\"tree tall\", \"pine tree\")"
Run Code Online (Sandbox Code Playgroud)

谢谢.

MrF*_*ick 34

您应该使用lapply小写列表中的每个字符向量

lapply(var1, tolower)

# [[1]]
# [1] "parts of a day"    "time in astronomy" "star"             
# 
# [[2]]
# [1] "tree tall" "pine tree"
Run Code Online (Sandbox Code Playgroud)

否则toloweras.character()你的整个名单是不是你想要的.

  • 为什么'sapply`而不是'lapply`?这里不需要简化. (7认同)