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:
- 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
- do this for every data frame.
- rbind all dataframe
- 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 " ".