Colleagues, I have created a list in the following manner: TEST <- list(c("A1", "A2"), c("B1", "B2"), c("C1", "C2")) I now want to delete one element from the list, e.g., the third. The command TEST[[3]] yields (as expected): [1] "C1" "C2" The command TEST[[-3]] yields: Error: attempt to select more than one element How can I accomplish delete one or more elements from this list? I am running R2.2.0 on a Linux platform. Dennis Dennis Fisher MD P < (The "P Less Than" Company) Phone: 1-866-PLessThan (1-866-753-7784) Fax: 1-415-564-2220 www.PLessThan.com [[alternative HTML version deleted]]
On 10/18/2005 10:11 AM, Dennis Fisher wrote:> Colleagues, > > I have created a list in the following manner: > TEST <- list(c("A1", "A2"), c("B1", "B2"), c("C1", "C2")) > > I now want to delete one element from the list, e.g., the third. The > command > TEST[[3]] > yields (as expected): > [1] "C1" "C2" > > The command > TEST[[-3]] > yields: > Error: attempt to select more than one element > > How can I accomplish delete one or more elements from this list?TEST[-3] does what you want. In general, single brackets say you want the answer to be a list, double brackets say you want to extract the element. Duncan Murdoch
You have to use "[" instead of "[[" to return a sub-list. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Dennis Fisher Sent: Tuesday, October 18, 2005 10:12 AM To: r-help at stat.math.ethz.ch Subject: [R] Subsetting a list Colleagues, I have created a list in the following manner: TEST <- list(c("A1", "A2"), c("B1", "B2"), c("C1", "C2")) I now want to delete one element from the list, e.g., the third. The command TEST[[3]] yields (as expected): [1] "C1" "C2" The command TEST[[-3]] yields: Error: attempt to select more than one element How can I accomplish delete one or more elements from this list? I am running R2.2.0 on a Linux platform. Dennis Dennis Fisher MD P < (The "P Less Than" Company) Phone: 1-866-PLessThan (1-866-753-7784) Fax: 1-415-564-2220 www.PLessThan.com [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Dennis Fisher wrote:>Colleagues, > >I have created a list in the following manner: > TEST <- list(c("A1", "A2"), c("B1", "B2"), c("C1", "C2")) > >I now want to delete one element from the list, e.g., the third. The >command > TEST[[3]] >yields (as expected): > [1] "C1" "C2" > >The command > TEST[[-3]] >yields: > Error: attempt to select more than one element > >How can I accomplish delete one or more elements from this list? > >I am running R2.2.0 on a Linux platform. > >Dennis > >Dennis Fisher MD >P < (The "P Less Than" Company) >Phone: 1-866-PLessThan (1-866-753-7784) >Fax: 1-415-564-2220 >www.PLessThan.com > > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > >TEST[[3]] <- NULL Lists are not subsetable like data.frames or arrays, see the manuals. EJ
you need single brackets, i.e., TEST <- list(c("A1", "A2"), c("B1", "B2"), c("C1", "C2")) TEST[-c(1, 3)] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Dennis Fisher" <fisher at plessthan.com> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, October 18, 2005 4:11 PM Subject: [R] Subsetting a list> Colleagues, > > I have created a list in the following manner: > TEST <- list(c("A1", "A2"), c("B1", "B2"), c("C1", "C2")) > > I now want to delete one element from the list, e.g., the third. > The > command > TEST[[3]] > yields (as expected): > [1] "C1" "C2" > > The command > TEST[[-3]] > yields: > Error: attempt to select more than one element > > How can I accomplish delete one or more elements from this list? > > I am running R2.2.0 on a Linux platform. > > Dennis > > Dennis Fisher MD > P < (The "P Less Than" Company) > Phone: 1-866-PLessThan (1-866-753-7784) > Fax: 1-415-564-2220 > www.PLessThan.com > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hi, you can use this: TEST[[3]]<-NULL> TEST[[1]] [1] "A1" "A2" [[2]] [1] "B1" "B2" Saludos Jaime Arturo Coral student PUCP - PERU On 10/18/05, Dennis Fisher <fisher@plessthan.com> wrote:> > Colleagues, > > I have created a list in the following manner: > TEST <- list(c("A1", "A2"), c("B1", "B2"), c("C1", "C2")) > > I now want to delete one element from the list, e.g., the third. The > command > TEST[[3]] > yields (as expected): > [1] "C1" "C2" > > The command > TEST[[-3]] > yields: > Error: attempt to select more than one element > > How can I accomplish delete one or more elements from this list? > > I am running R2.2.0 on a Linux platform. > > Dennis > > Dennis Fisher MD > P < (The "P Less Than" Company) > Phone: 1-866-PLessThan (1-866-753-7784) > Fax: 1-415-564-2220 > www.PLessThan.com <http://www.PLessThan.com> > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Saludos cordiales J Arturo Coral [[alternative HTML version deleted]]
Dennis ---- Try> TEST[-3][[1]] [1] "A1" "A2" [[2]] [1] "B1" "B2" for removing more than one element from the list (say 2 & 3) --> TEST[-c(2,3)][[1]] [1] "A1" "A2" HTH John Dennis Fisher wrote--- Colleagues, I have created a list in the following manner: TEST <- list(c("A1", "A2"), c("B1", "B2"), c("C1", "C2")) I now want to delete one element from the list, e.g., the third. The command TEST[[3]] yields (as expected): [1] "C1" "C2" The command TEST[[-3]] yields: Error: attempt to select more than one element How can I accomplish delete one or more elements from this list? I am running R2.2.0 on a Linux platform. Dennis