I have a list:
mylist<-list()
data<-1:162
listlabel<-c("a","bpt","b","fpt")
for (i in 1:4){
label<-listlabel[i]
mylist[[label]]<-data
}
I want to create a new list from the elements in my list that contain a certain string: in this case 'pt'. My new list should contain the elements 'bpt' and 'fpt'.
I thought 'grep' would do it, but when I try it, it returns an empty list.
newlist<-list(grep("pt",mylist))
How can I create the new list keeping only the selected elements containing my string?
The grep
should be on the names
and not the values of the list
mylist_sub <- mylist[grep('pt', names(mylist))]