I have a list of 7 elements. the element names have the pattern p1.abc1 , p1.abc2, p1.abc3 , p2.abc1, p2.abc2,p2.abc3, p2.abc4.
Data
a.list<- list(p1.abc1= c(1:3), p1.abc2=4, p1.abc3=rep(2,4), p2.abc1=1:3, p2.abc2=c(3:7), p2.abc3=rep(9,3),p2.abc4= c(5:9))
I want to sub list this list to become of length 2.
the expected result is b.list as in the image below:
I have tried the following code but it did not work:
how can I put these two syntax in loop
a.list[grep("p1.abc", names(a.list))]
a.list[grep("p2.abc", names(a.list))]
b.list<-for( i in 1:2){
a.list[grep("p[i].abc", names(a.list))]
}