Rename columns in nested lists and row bind

Viewed 1759

I've a nested list of objects that I'd like to first rename some variables and row bind its object, but selecting only some variables. In the example below, I'd like to rename columns A to a in the second object, and w to x in the third object to, then row bind all three object selecting only columns a and x using.

Data:

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)))

List structure:

   > lapply(df, names)
[[1]]
[1] "a" "x" "y"

[[2]]
[1] "A" "x"

[[3]]
[1] "a" "w" "y"

Then, row binding then:

library(plyr)
df2 <- ldply(df, data.frame)
3 Answers
Related