对于以下数据表,如果列水果包含“ apple”或“ orange”,我想重命名列水果,即,我想在DT中而不是DT2中重命名列水果。
library(data.table)
DT <- data.table(colour = c("green", "red", "red", "red", "blue","red"), fruit = c("apple", "orange", "pear", "apple", "apple","banana"))
DT2 <- data.table(colour = c("green", "red", "red", "red", "blue","red"), fruit = c("pear", "pear", "pear", "banana", "pear","banana"))
我想先搜索数据表中是否包含水果列,所以我尝试了下面的代码,但是它在DT和DT2中重命名了水果:
alist <- list(DT, DT2)
lapply(alist, function(x) {
if ("fruit" %in% colnames(x)){
x <- x[fruit=="apple"|fruit=="orange", setnames(x, old="fruit", new="appfruit")]
x}})
任何帮助将不胜感激。
答案 0 :(得分:1)
将功能更改为:
lapply(alist, function(x) {
if(any(x[["fruit"]] %in% c("apple","orange"))) {
setnames(x, old = "fruit", new = "appfruit")
}}
)
将给出预期的结果(有关扩展的示例数据,请参见下文):
> alist [[1]] colour appfruit 1: green apple 2: red orange 3: red pear 4: red apple 5: blue apple 6: red banana [[2]] colour fruit 1: green pear 2: red pear 3: red pear 4: red banana 5: blue pear 6: red banana [[3]] colour veggie 1: green apple 2: red pear 3: red pear 4: red banana 5: blue pear 6: red banana
如您所见,当没有fruit
列时,列名不会更改。
使用的数据:
DT1 <- data.table(colour = c("green", "red", "red", "red", "blue","red"), fruit = c("apple", "orange", "pear", "apple", "apple","banana"))
DT2 <- data.table(colour = c("green", "red", "red", "red", "blue","red"), fruit = c("pear", "pear", "pear", "banana", "pear","banana"))
DT3 <- data.table(colour = c("green", "red", "red", "red", "blue","red"), veggie = c("apple", "pear", "pear", "banana", "pear","banana"))
alist <- list(DT1, DT2, DT3)