[R] Filtering out data from list
arun
smartpink111 at yahoo.com
Sat Sep 21 22:43:36 CEST 2013
Hi,
list1<-as.list(c("hi I am here!","my name is bob!", "I am mary!!", "bob likes mary!!"))
list2<- list1[grepl("bob",unlist(list1))]
list2
#[[1]]
#[1] "my name is bob!"
#
#[[2]]
#[1] "bob likes mary!!"
A.K.
Hm thanks :P
but i was actually trying to say is like...
if my list were more random like below and i'm trying to filter out this list which doesnt have the
specific word such as bob.
> list1
[[1]]
[1] "hi I am here!"
[[2]]
[1] "my name is bob!"
[[3]]
[1] "I am mary!!"
[[4]]
[1] "bob likes mary!!"
will become
> list1
[[1]]
[1] "my name is bob!"
[[2]]
[1] "bob likes mary!!"
----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc:
Sent: Saturday, September 21, 2013 12:08 PM
Subject: Re: Filtering out data from list
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