我有一个嵌套的对象列表,我想首先重命名一些变量并对其绑定对象,但只选择一些变量。
在下面的示例中,我想在第二个对象中将列A
重命名为a
,在第三个对象中将w
重命名为x
,然后排绑定所有三个对象,仅选择列a
和x
。
数据:
df <- list(structure(list(a = 1:3,
x = c(-1.99, -1.11, -0.34),
y = c("C", "B", "A")), .Names = c("a", "x", "y"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L)), structure(list(a = 1:3, x = c(-0.44, -1.07, -0.23)), .Names = c("A", "x"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L)),
structure(list(a = 1:3, x = c(-0.62, -0.60, -0.06),
y = c(3L, 2L, 1L)), .Names = c("a", "w", "y"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L)))
列表结构:
> lapply(df, names)
[[1]]
[1] "a" "x" "y"
[[2]]
[1] "A" "x"
[[3]]
[1] "a" "w" "y"
然后,行绑定:
library(plyr)
df2 <- ldply(df, data.frame)
答案 0 :(得分:1)
您可以通过以下方式实现这一目标:
library(plyr)
df = lapply(df, function(x) {plyr::rename(x,c("A"="a","w"="x"),warn_missing = F)})
df2 <- ldply(lapply(df, function(x) {x[,c("a","x")]}), data.frame)
输出:
a x
1 1 -1.99
2 2 -1.11
3 3 -0.34
4 1 -0.44
5 2 -1.07
6 3 -0.23
7 1 -0.62
8 2 -0.60
9 3 -0.06
希望这有帮助。
答案 1 :(得分:0)
使用purrr
(map
),dplyr
(rename
,select
,bind_rows
,%>%
)和magrittr( %<>%
,%>%
)):
library(purrr)
library(dplyr)
library(magrittr)
df[[2]] %<>% rename(.,a = A)
df[[3]] %<>% rename(.,x = w)
df %>% map_df(. %>% select("a","x"))
# # A tibble: 9 x 2
# a x
# <int> <dbl>
# 1 1 -1.99
# 2 2 -1.11
# 3 3 -0.34
# 4 1 -0.44
# 5 2 -1.07
# 6 3 -0.23
# 7 1 -0.62
# 8 2 -0.60
# 9 3 -0.06
或在基地R:
names(df[[2]])[names(df[[2]]) == "A"] <- "a"
names(df[[3]])[names(df[[3]]) == "w"] <- "x"
do.call(rbind,lapply(df,"[",c("a","x")))
答案 2 :(得分:0)
另一个想法可能是创建一个带有替换值的命名向量v
,遍历列表,如果匹配则重命名并选择所需的列。
v <- c("a" = "A", "x" = "w")
map_df(df, .f = ~ rename_if(
.x,
.p = names(.x) %in% v,
.f = funs(stringi::stri_replace_all_fixed(., v, names(v), vectorize_all = FALSE))) %>%
select(names(v))
)
给出了:
## A tibble: 9 x 2
# a x
# <int> <dbl>
#1 1 -1.99
#2 2 -1.11
#3 3 -0.34
#4 1 -0.44
#5 2 -1.07
#6 3 -0.23
#7 1 -0.62
#8 2 -0.60
#9 3 -0.06