Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append to a list from multiple list in r?

Tags:

list

append

r

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"
like image 285
Bharat Desai Avatar asked Oct 27 '25 07:10

Bharat Desai


1 Answers

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))
like image 182
1.618 Avatar answered Oct 29 '25 21:10

1.618