list R 将列表转换为小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30429039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:07:50  来源:igfitidea点击:

R Convert list to lowercase

rlistlowercase

提问by user6633625673888

Var1 is a list:

Var1 是一个列表:

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.

谢谢。

回答by MrFlick

You should use lapplyto lower case each character vector in your list

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

lapply(var1, tolower)

# [[1]]
# [1] "parts of a day"    "time in astronomy" "star"             
# 
# [[2]]
# [1] "tree tall" "pine tree"

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

否则tolower会出现as.character()在你的整个列表中,这不是你想要的。

回答by Josh Stevens

Use gsub

gsub

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

this will remove all your / out of your variable.

这将从您的变量中删除所有您的 / 。