Jeni
Jeni

Reputation: 958

Merge several data frames named inside a vector

I am trying to merge some data frames, precisely 4, but I would like my command to work with whichever amount of them. Those data frames are named in a vector:

dataframes<-c('df1', 'data2', 'd3', 'samples4')

All data frames present the same data, but they belong to different samples. As an example, the first dataframe is as following:

ID  count1

A     0
B     67
C     200
D     12
E     0

My desired output would contain a column with the counts of each ID for each sample:

ID  count1  count2 count3  count4

A     0       2       0     30
B     67     100     300    500
C     200     2      1025   0
D     12      4       0     10
E     0       0       20    2

I have tried the following commands:

Reduce(function(x, y) merge(x, y, by="ID"), list(unname(get(dataframes))))

as.data.frame(do.call(cbind, unname(get(dataframes))))

But in both cases I get just the first data frame. No merging is occuring.

How can I solve this?

Upvotes: 0

Views: 65

Answers (1)

matt_jay
matt_jay

Reputation: 1271

Assuming:

df1 <- data.frame(ID = c("A", "B", "C", "D"), count = c(2,45,24,21))
df2 <- data.frame(ID = c("A", "B", "C", "D"), count = c(11,35,4,2))

I'd suggest to just add a column with the sample name to each dataframe, e.g.:

df1["sample"] <- "sample1"
df2["sample"] <- "sample2"

Then merge them "vertically" by using something like rbind().

all_data <- rbind(df1, df2)  # this can take more dataframes

This "long format" should also make it easier to filter rows by sample.

If you still need to have the wider structure you describe above (with a column for each sample), you can use reshape2::dcast() to construct it:

library(reshape2)
all_data <– dcast(all_data, ID ~ sample, value.var="count")

Result:

  ID sample1 sample2
1  A       2      11
2  B      45      35
3  C      24       4
4  D      21       2

Upvotes: 1

Related Questions