I have 4 lists: 3 equally sized filled lists and 1 empty list, for example:
list1<-as.list(c(1:10))
list2<-as.list(c(101:110))
list3<-as.list(c(1001:1010))
list4<-list()
I want to append the ith element of each of the first 3 lists into the 4th list. list 4 should look like:
[1,101,1001,2,102,1002,....,10,110,1010]
How do i go about doing this? My code currently looks like this:
for (i in length(list1)){
local({
i<-i
list4.append(list1[i])
list4.append(list2[i])
list4.append(list3[i])
})
}
but i receive the error:
could not find function "list4.append"
You can use mapply to combine multiple lists into one. The function used within mapply is c
list4 <- mapply(c, list1, list2, list3, SIMPLIFY = F)
Sample data:
list1 <- as.list(c(1:10))
list2 <- as.list(c(101:110))
list3 <- as.list(c(1001:1010))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With