I have a variation on the oh-so-common problem of how to merge things together in R.

I have a set of .txt files in a particular folder, and I have written a function that:

  • makes a list of the files I want, and then for each file
  • reads the file
  • subsets the data (to extract just the rows and columns of interest)
  • does some calculations on the data
  • adds these new values to a list.

What I end up with is a list with the following structure:

>str(DataList)
List of 16
 $ :'data.frame':   14 obs. of  2 variables:
  ..$ Sample: Factor w/ 14 levels "Sample_1A","Sample_1B",..: 1 2 3 4 5 6 7 8 9 10 ...
  ..$ Var1  : num [1:14] 27.9 33.8 29.9 29.4 28.8 ...
 $ :'data.frame':   14 obs. of  2 variables:
  ..$ Sample: Factor w/ 14 levels "Sample_1A","Sample_1B",..: 1 2 3 4 5 6 7 8 9 10 ...
  ..$ Var2  : num [1:14] 24.6 27 26.8 26.7 27.2 ...
 $ :'data.frame':   12 obs. of  2 variables:
  ..$ Sample: Factor w/ 14 levels "Sample_1A","Sample_1B",..: 1 2 3 4 5 6 7 9 11 12 ...
  ..$ Var3  : num [1:12] 31.4 35.6 34 35.7 32.5 ...

For each variable (Var1, Var2, Var3, ...) I have a column Sample and a column of numerical values.

Sample is always a factor with 14 levels; these levels are the same for each variable.

The problem is that some variables (like Var3 above) don't have observations for each level of Sample.

What I want to end up with is a data frame with 14 rows (one for each level of Sample). The first column should be Sample; then for each variable, there should be a column containing the corresponding numerical values, like so:

Sample     Var1    Var2    Var3
Sample_1A  27.9    24.6    31.4
Sample_1B  33.8    27      35.6
...
Sample_3B  26.8    29.7    NA

I've been trying to do this with do.call, but

Edit
Report