Min-Su
Min-Su

Reputation: 15

r rbind dataframes in each list using lapply function

I want to add some data points. odtl is the original data andadtl is the data points to add. adtl is set to NA but will be interpolated by zoo :: na.spline after rbind.
During this process, two lists(odtl and adtl) contain three data frames each. I want to combine the data frames in the order in which they are loaded into each list. I succeed this using the for function as follows. But my lapply function doesn't work. Could you make this loop as a lapply or apply family functions? Thanks.

> odtl # original dataset
[[1]]
  x index
1 1     1
2 2     2
3 3     3
4 4     4
5 5     5

[[2]]
  x index
1 1     1
2 2     2
3 3     3
4 4     4

[[3]]
  x index
1 1     1
2 2     2
3 3     3

> adtl # dataset for add
[[1]]
   x index
1 NA   1.5

[[2]]
   x index
1 NA   1.5
2 NA   2.5
3 NA   3.5

[[3]]
   x index
1 NA   1.5
2 NA   2.5

> wdtl <- list() # This is the goal.
> for(i in 1:length(odtl)){
+   wdtl[[i]] <- rbind(odtl[[i]], adtl[[i]])
+ }
> wdtl # This is the goal but I want complete it by lapply or something
[[1]]
   x index
1  1   1.0
2  2   2.0
3  3   3.0
4  4   4.0
5  5   5.0
6 NA   1.5

[[2]]
   x index
1  1   1.0
2  2   2.0
3  3   3.0
4  4   4.0
5 NA   1.5
6 NA   2.5
7 NA   3.5

[[3]]
   x index
1  1   1.0
2  2   2.0
3  3   3.0
4 NA   1.5
5 NA   2.5

Upvotes: 0

Views: 1641

Answers (3)

warnbergg
warnbergg

Reputation: 552

Specifically from the lapply, apply etc. family of functions, you could use mapply

> odtl  <- list(data.frame(x=1:5, index=1:5),
                data.frame(x=1:4, index=1:4),
                data.frame(x=1:3, index=1:3))
> adtl  <- list(data.frame(x=NA, index=seq(1.5, 5.5, 1)),
                data.frame(x=NA, index=seq(1.5, 4.5, 1)),
                data.frame(x=NA, index=seq(1.5, 3.5, 1)))v
> mapply(rbind, odtl, adtl, SIMPLIFY = FALSE)
# [[1]]
#     x index
# 1   1   1.0
# 2   2   2.0
# 3   3   3.0
# 4   4   4.0
# 5   5   5.0
# 6  NA   1.5
# 7  NA   2.5
# 8  NA   3.5
# 9  NA   4.5
# 10 NA   5.5
# 
# [[2]]
#    x index
# 1  1   1.0
# 2  2   2.0
# 3  3   3.0
# 4  4   4.0
# 5 NA   1.5
# 6 NA   2.5
# 7 NA   3.5
# 8 NA   4.5
# 
# [[3]]
#    x index
# 1  1   1.0
# 2  2   2.0
# 3  3   3.0
# 4 NA   1.5
# 5 NA   2.5
# 6 NA   3.5

Note that Map is a wrapper around mapply(FUN = f, ..., SIMPLIFY = FALSE).

Upvotes: 0

jay.sf
jay.sf

Reputation: 72663

You may use Map() which element-wise applies a function to the first elements of each of its arguments.

Map(rbind, odtl, adtl)
# [[1]]
# x index
# 1   1   1.0
# 2   2   2.0
# 3   3   3.0
# 4   4   4.0
# 5   5   5.0
# 6  NA   1.5
# 7  NA   2.5
# 8  NA   3.5
# 9  NA   4.5
# 10 NA   5.5
# 
# [[2]]
# x index
# 1  1   1.0
# 2  2   2.0
# 3  3   3.0
# 4  4   4.0
# 5 NA   1.5
# 6 NA   2.5
# 7 NA   3.5
# 8 NA   4.5
# 
# [[3]]
# x index
# 1  1   1.0
# 2  2   2.0
# 3  3   3.0
# 4 NA   1.5
# 5 NA   2.5
# 6 NA   3.5

Data

odtl  <- list(data.frame(x=1:5, index=1:5),
              data.frame(x=1:4, index=1:4),
              data.frame(x=1:3, index=1:3))
adtl  <- list(data.frame(x=NA, index=seq(1.5, 5.5, 1)),
              data.frame(x=NA, index=seq(1.5, 4.5, 1)),
              data.frame(x=NA, index=seq(1.5, 3.5, 1)))

Upvotes: 1

ThomasIsCoding
ThomasIsCoding

Reputation: 101179

I think the solution in the comments by @thelatemail should be the most elegant one. If you want to use lapply, then the below would be the something you want

wdtl <- sapply(seq(odtl), function(k) rbind(odtl[[k]],adtl[[k]]))

Upvotes: 0

Related Questions