Hello I am am new to R and any help with the following would be appreciated: I have a list (example attached) and I would like to create a new list which is a filtered version of this list. I.e I would like a list that only contains elements with this value: Chr 10 : 21853562 - 21855482 Any pointers/tips would be great. Thanks! Nikol -- Bioinformatician/Computer Associate Cambridge Institute for Medical Research University of Cambridge Wellcome Trust/MRC Building Hills Road Cambridge CB2 0XY 01223 762111 -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: list.txt URL: <stat.ethz.ch/pipermail/r-help/attachments/20090304/e7687561/attachment-0003.txt>
Hi r-help-bounces at r-project.org napsal dne 04.03.2009 13:18:30:> Hello > I am am new to R and any help with the following would be appreciated: > I have a list (example attached) and I would like to create a new list > which is a filtered version of this list. I.e I would like a list that > only contains elements with this value: > > Chr 10 : 21853562 - 21855482Lists can have quite complicated structure so here is only a small suggestion lapply(your.list, function(x) x==desired.value) This shall give you list of logical values if your list has only one level. Something like pts <- list(x=mtcars[,1], y=mtcars[,2], z=mtcars[,10]) pts[which(unlist(lapply(pts, function(x) sum(x==4)>0)))] Regards Petr> > Any pointers/tips would be great. > Thanks! > Nikol > > -- > Bioinformatician/Computer Associate > Cambridge Institute for Medical Research > University of Cambridge > Wellcome Trust/MRC Building > Hills Road > Cambridge > CB2 0XY > 01223 762111 > > [[1]] > human../US45102804_251757910002_S01_CGH-v4_95_Feb07.chr10.cher1 > Chr 10 : 21853562 - 21855482 > Antibody : ./US45102804_251757910002_S01_CGH-v4_95_Feb07 > Maximum level = 0.5370385 > Score = 3.193875 > Spans 19 probes. > Defined extras: typeUpstream, typeInside, distMid2TSS, upSymbol,inSymbol, type> > [[2649]] > human../US45102804_252058310010_S01_CGH-v4_95_Feb07.chrX.cher18 > Chr X : 72387424 - 72387712 > Antibody : ./US45102804_252058310010_S01_CGH-v4_95_Feb07 > Maximum level = 0.3504706 > Score = 0.4045575 > Spans 4 probes. > Defined extras: typeUpstream, typeInside, distMid2TSS, upSymbol,inSymbol, type> > [[2650]] > human../US45102804_252058310010_S01_CGH-v4_95_Feb07.chrX.cher19 > Chr X : 129037305 - 129037494 > Antibody : ./US45102804_252058310010_S01_CGH-v4_95_Feb07 > Maximum level = 0.4718157 > Score = 0.5751177 > Spans 3 probes. > Defined extras: typeUpstream, typeInside, distMid2TSS, upSymbol,inSymbol, type> > [[2651]] > human../US45102804_252058310010_S01_CGH-v4_95_Feb07.chrX.cher20 > Chr X : 129048386 - 129048865 > Antibody : ./US45102804_252058310010_S01_CGH-v4_95_Feb07 > Maximum level = 0.3711013 > Score = 0.4121159 > Spans 5 probes. > Defined extras: typeUpstream, typeInside, distMid2TSS, upSymbol,inSymbol, type> > [[2652]] > human../US45102804_252058310010_S01_CGH-v4_95_Feb07.chrX.cher21 > Chr X : 129065300 - 129066038 > Antibody : ./US45102804_252058310010_S01_CGH-v4_95_Feb07 > Maximum level = 0.4387153 > Score = 1.515073 > Spans 8 probes. > Defined extras: typeUpstream, typeInside, distMid2TSS, upSymbol,inSymbol, type> ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guideR-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Dear Nikol, Try this: do.call(c,lapply(yourlist,function(x) x[2] )) HTH, Jorge On Wed, Mar 4, 2009 at 7:18 AM, Nikol Simecek <ns455@cam.ac.uk> wrote:> Hello > I am am new to R and any help with the following would be appreciated: > I have a list (example attached) and I would like to create a new list > which is a filtered version of this list. I.e I would like a list that only > contains elements with this value: > > Chr 10 : 21853562 - 21855482 > > Any pointers/tips would be great. > Thanks! > Nikol > > -- > Bioinformatician/Computer Associate > Cambridge Institute for Medical Research > University of Cambridge > Wellcome Trust/MRC Building > Hills Road > Cambridge > CB2 0XY > 01223 762111 > > [[1]] > human../US45102804_251757910002_S01_CGH-v4_95_Feb07.chr10.cher1 > Chr 10 : 21853562 - 21855482 > Antibody : ./US45102804_251757910002_S01_CGH-v4_95_Feb07 > Maximum level = 0.5370385 > Score = 3.193875 > Spans 19 probes. > Defined extras: typeUpstream, typeInside, distMid2TSS, upSymbol, inSymbol, > type > > [[2649]] > human../US45102804_252058310010_S01_CGH-v4_95_Feb07.chrX.cher18 > Chr X : 72387424 - 72387712 > Antibody : ./US45102804_252058310010_S01_CGH-v4_95_Feb07 > Maximum level = 0.3504706 > Score = 0.4045575 > Spans 4 probes. > Defined extras: typeUpstream, typeInside, distMid2TSS, upSymbol, inSymbol, > type > > [[2650]] > human../US45102804_252058310010_S01_CGH-v4_95_Feb07.chrX.cher19 > Chr X : 129037305 - 129037494 > Antibody : ./US45102804_252058310010_S01_CGH-v4_95_Feb07 > Maximum level = 0.4718157 > Score = 0.5751177 > Spans 3 probes. > Defined extras: typeUpstream, typeInside, distMid2TSS, upSymbol, inSymbol, > type > > [[2651]] > human../US45102804_252058310010_S01_CGH-v4_95_Feb07.chrX.cher20 > Chr X : 129048386 - 129048865 > Antibody : ./US45102804_252058310010_S01_CGH-v4_95_Feb07 > Maximum level = 0.3711013 > Score = 0.4121159 > Spans 5 probes. > Defined extras: typeUpstream, typeInside, distMid2TSS, upSymbol, inSymbol, > type > > [[2652]] > human../US45102804_252058310010_S01_CGH-v4_95_Feb07.chrX.cher21 > Chr X : 129065300 - 129066038 > Antibody : ./US45102804_252058310010_S01_CGH-v4_95_Feb07 > Maximum level = 0.4387153 > Score = 1.515073 > Spans 8 probes. > Defined extras: typeUpstream, typeInside, distMid2TSS, upSymbol, inSymbol, > type > > ______________________________________________ > R-help@r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >[[alternative HTML version deleted]]
Nikol Simecek wrote:> Hello > I am am new to R and any help with the following would be appreciated: > I have a list (example attached) and I would like to create a new list > which is a filtered version of this list. I.e I would like a list that > only contains elements with this value: > > Chr 10 : 21853562 - 21855482Filter provides a generic approach to filtering: Filter(x=<your list>, f=function(element) <does element contain 'Chr 10 : 21853562 - 21855482'?>) where you have to implement the filtering function. if each element in your list is a string (a character vector of length 1), you can also use grep: grep('Chr 10 : 21853562 - 21855482', <your list>, value=TRUE) if each element in your list is a character vector, a list, a data frame, or something else (not just a single string), you may use a combination of grep and lapply, but i think the Filter approach is in this case cleaner. see ?Filter and ?grep. vQ
Petr PIKAL wrote:> Hi > > > r-help-bounces at r-project.org napsal dne 04.03.2009 13:18:30: > > >> Hello >> I am am new to R and any help with the following would be appreciated: >> I have a list (example attached) and I would like to create a new list >> which is a filtered version of this list. I.e I would like a list that >> only contains elements with this value: >> >> Chr 10 : 21853562 - 21855482 >> > > Lists can have quite complicated structure so here is only a small > suggestion > > lapply(your.list, function(x) x==desired.value) > > This shall give you list of logical values if your list has only one > level. > > Something like > > pts <- list(x=mtcars[,1], y=mtcars[,2], z=mtcars[,10]) > pts[which(unlist(lapply(pts, function(x) sum(x==4)>0)))] >pts[sapply(pts, function(x) sum(x==4)>0)] will gladly do the same. vQ
Possibly Parallel Threads
- subseting a data frame
- How do I juxtapose two lattice graphs with common X axes such that the X axes line up?
- average two sets of columns
- Qs re writing/reading arrays, dataframes
- How best to get around shadowing of executables by system()'s prepending of directories to Windows' PATH?