[R] Filtering out data from list

arun smartpink111 at yahoo.com
Sat Sep 21 18:08:15 CEST 2013


Hi,
 list1<-lapply(c("",2:3),function(x) paste0("hi I am here!",x))
#or
list1<-  as.list(paste0("hi I am here!",c("",2:3)))


 str(list1)
#List of 3
# $ : chr "hi I am here!"
# $ : chr "hi I am here!2"
# $ : chr "hi I am here!3"


It is not a list within a list
 

list2<-list1[grepl("2",unlist(list1))]
 list2
#[[1]]
#[1] "hi I am here!2"



#list within a list
listNew<- list( as.list(paste0("hi I am here!",c("",2:3))))
str(listNew)
#List of 1
# $ :List of 3
 # ..$ : chr "hi I am here!"
  #..$ : chr "hi I am here!2"
  #..$ : chr "hi I am here!3"

listNew2<- lapply(1:3,function(x) as.list(paste0("hi I am here!",c("",2:3))))
 str(listNew2)
#List of 3
# $ :List of 3
 # ..$ : chr "hi I am here!"
  #..$ : chr "hi I am here!2"
  #..$ : chr "hi I am here!3"
 #$ :List of 3
 # ..$ : chr "hi I am here!"
 # ..$ : chr "hi I am here!2"
 # ..$ : chr "hi I am here!3"
# $ :List of 3
#  ..$ : chr "hi I am here!"
#  ..$ : chr "hi I am here!2"
#  ..$ : chr "hi I am here!3"


A.K.




Okay im pretty new in r...   
and im having trouble figuring how to filtering out data 
just say for example i have a list which prints out 

> list1 
[[1]] 
[1] "hi I am here!" 

[[2]] 
[1] "hi I am here!2" 

[[3]] 
[1] "hi I am here!3" 

.... 

1) okay first I need to confirm , is that a list within a list ? since its displaying 

[[1]] 
[1]    <-- in this fashion 

2) and now just say i want to have list1 to only contain the list with the number 2 in it how do i do it? 
or any methods to suggest ?



More information about the R-help mailing list