这应该是显而易见的,但我还是没有得到我的脑海围绕lapply
及其亲属,而且我还没有找到一个明确而简单的答案,这对SO或Google。因此,与道歉noobness,这里有云:列名tolower的()递归(在dataframes列表)
我有多重的嵌入列表,有点像这样(抓获来自here重复的例子):
lst <- structure(list(Df1 = structure(list(Df1 = structure(list(Date = structure(c(14611, 14612), class = "Date"), Ta_200 = c(10.0067787761421, 5.9095282339839)), .Names = c("Date", "Ta_200"), row.names = c(NA, -2L), class = "data.frame"), Df2 = structure(list(Date = structure(c(14611, 14612), class = "Date"), rH_200 = c(64.9115310510325, 90.8615907551521)), .Names = c("Date", "rH_200"), row.names = c(NA, -2L), class = "data.frame")), .Names = c("Df1", "Df2")), Df2 = structure(list(Df1 = structure(list(Date = structure(c(14642, 14643), class = "Date"), Ta_200 = c(9.91976687351846, 8.79129183854663)), .Names = c("Date", "Ta_200"), row.names = c(NA, -2L), class = "data.frame"), Df2 = structure(list(Date = structure(c(14642, 14643), class = "Date"), rH_200 = c(76.9297879127307, 75.8021788747459)), .Names = c("Date", "rH_200"), row.names = c(NA, -2L), class = "data.frame")), .Names = c("Df1", "Df2"))), .Names = c("Df1", "Df2"))
的目标是:让所有列名都使用tolower()
,为2或3级深度的递归列表(如此处)使用小写字母。我可以遍历列表元素,但我试图按照我在任何地方看到的建议来避免循环,并使用apply
系列和dplyr
中的东西。
我可以使用lapply
第二水平下降这样的:
lapply(lst, function(x) { names(x) <- tolower(names(x)) })
但是:(1)我不知道如何递归的第一和第二(和第三)水平做到这一点, (2)我很想念如何实际将新的小写名称写入列表项(上面的行只返回它们)。
'setNames'是有帮助的:'lapply(LST,函数(X){setNames(X,tolower的(名称(X)))})'这只做一次在一个级别,虽然。 – alistaire