0

I want to create a dataframe from a list of dataframes, specifically from a certain column of those dataframes. However each dataframe contains a different number of observations, so the following code gives me an error.

diffs <- data.frame(sensor1 = sensores[[1]]$Diff,
                    sensor2 = sensores[[2]]$Diff,
                    sensor3 = sensores[[3]]$Diff,
                    sensor4 = sensores[[4]]$Diff,
                    sensor5 = sensores[[5]]$Diff)

The error:

Error in data.frame(sensor1 = sensores[[1]]$Diff, sensor2 = sensores[[2]]$Diff,  : 
arguments imply differing number of rows: 29, 19, 36, 26

Is there some way to force data.frame() to take the minimal number or rows available from each one of the columns, in this case 19?

Maybe there is a built-in function in R that can do this, any solution is appreciated but I'd love to get something as general and as clear as possible.

Thank you in advance.

Maganna Dev
  • 189
  • 1
  • 11

1 Answers1

1

I can think of two approaches:

Example data:

df1 <- data.frame(A = 1:3)
df2 <- data.frame(B = 1:4)
df3 <- data.frame(C = 1:5)

Compute the number of rows of the smallest dataframe:

min_rows <- min(sapply(list(df1, df2, df3), nrow))

Use subsetting when combining:

diffs <- data.frame(a = df1[1:min_rows,], b = df2[1:min_rows,], c = df3[1:min_rows,] )
diffs
  a b c
1 1 1 1
2 2 2 2
3 3 3 3

Alternatively, use merge:

rowmerge <- function(x,y){
   # create row indicators for the merge:
   x$ind <- 1:nrow(x)
   y$ind <- 1:nrow(y)
   out <- merge(x,y, all = T, by = "ind")
   out["ind"] <- NULL
   return(out)
}
Reduce(rowmerge, list(df1, df2, df3))
   A  B C
1  1  1 1
2  2  2 2
3  3  3 3
4 NA  4 4
5 NA NA 5

To get rid of the rows with NAs, remove the all = T.

For your particular case, you would probably call Reduce(rowmerge, sensores), assuming that sensores is a list of dataframes.

Note: if you already have an index somewhere (e.g. a timestamp of some sort), then it would be advisable to simply merge on that index instead of creating ind.

coffeinjunky
  • 11,254
  • 39
  • 57
  • That did the trick. Your first approach was what I was thinking about, but I wasn't sure how to do it. The second approach yields an error, I'll look deeper into it later. – Maganna Dev Aug 04 '18 at 17:58
  • @MagannaDev What error does the second approach give you? – coffeinjunky Aug 04 '18 at 18:51