1

Given: a list of data frames with the same number of columns, but varying number of rows.
Trying to get: a function that extracts the same column from all data frames and generates another column that labels from which data frame the common column came from.

My reasoning:

  1. Use column I want and the name of the data frame to make a new data frame that has two columns: column of labels (name of the dataframe) and the column of interest
  2. do this for every data frame.
  3. rbind all dataframe
  4. Make a function that does this for as many data frame inputs as requested

Given:

a <- data.frame(V1=c(1:3),V2=c(1001:1003))
b <- data.frame(V1=c(1:5),V2=c(2001:2005))

What I want:

rbind(data.frame(group="a",value=c(a$V2)),data.frame(group="b",value=c(b$V2)))

Effort to make a function that does this:

my_fn <- function(...) {
  arg <- structure(list(...),names=as.list(substitute(list(...)))[-1L])
  do.call(rbind,lapply(arg,function(x) {data.frame(group="x",value=c(x$V2))})) %>% return
}

In the function I tried, I can almost get what I want, except group="x" is read as "x". But I want it to read it as x=na me of object in the list and then put " ".

Brigadeiro
  • 2,649
  • 13
  • 30
Jun
  • 11
  • 1
  • You might use `data.table::rbindlist(list(a, b), idcol = "group")` or `dplyr::bind_rows(list(a, b), .id = "group")` – markus Jul 31 '19 at 19:34
  • Markus's suggestion combined with [something like this to preserve the names when you call `list()`](https://stackoverflow.com/a/16951524/903061) should be pretty simple and work well – Gregor Thomas Jul 31 '19 at 19:39
  • `tibble::lst` creates a list from variables where variable names become the names in the list, and `dplyr::bind_rows` has an argument `.id` that creates a column with the data frames' names as ID values – camille Jul 31 '19 at 20:09

0 Answers0