[R] remove component from list or data frame
Sorry to ask such a simple question, but I can't find the answer after extensive searching the docs and the web. How do you remove a component from a list? For example say you have: lst-c(5,6,7,8,9) How do you remove, for example, the third component in the list? lst[[3]]]-NULL generates an error: Error: more elements supplied than there are to replace Also, how do you remove a row from a data frame? For example, say you have: lst1-c(1,2,3,4,5) lst2-c(6,7,8,9,10) frame-data.frame(lst1,lst2) How do you remove, for example, the second row of frame? Thanks, - Jason __ 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] remove component from list or data frame
The first example you provide is a vector, not a list. You can remove the third element with: lst[-3] [1] 5 6 8 9 The same thing works for rows of data frames: frame[-3,] lst1 lst2 116 227 449 55 10 On 08/02/07, Jason Horn [EMAIL PROTECTED] wrote: Sorry to ask such a simple question, but I can't find the answer after extensive searching the docs and the web. How do you remove a component from a list? For example say you have: lst-c(5,6,7,8,9) How do you remove, for example, the third component in the list? lst[[3]]]-NULL generates an error: Error: more elements supplied than there are to replace Also, how do you remove a row from a data frame? For example, say you have: lst1-c(1,2,3,4,5) lst2-c(6,7,8,9,10) frame-data.frame(lst1,lst2) How do you remove, for example, the second row of frame? Thanks, - Jason __ 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 and provide commented, minimal, self-contained, reproducible code. -- = David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP -- = David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP __ 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] remove component from list or data frame
Hi Jason, On 2/8/07, Jason Horn [EMAIL PROTECTED] wrote: Sorry to ask such a simple question, but I can't find the answer after extensive searching the docs and the web. How do you remove a component from a list? For example say you have: You use the - operator for both your vector and data frame examples. lst - c(5,6,7,8,9) # which by the way isn't a list is.list(lst) [1] FALSE lst [1] 5 6 7 8 9 lst - lst[-3] lst [1] 5 6 8 9 lst1-c(1,2,3,4,5) lst2-c(6,7,8,9,10) frame-data.frame(lst1,lst2) frame lst1 lst2 116 227 338 449 55 10 frame[-2,] lst1 lst2 116 338 449 55 10 -- Sarah Goslee http://www.functionaldiversity.org __ 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] remove component from list or data frame
First: lst - lst[-3] Second: frame- frame[-2,] On 08/02/07, Jason Horn [EMAIL PROTECTED] wrote: Sorry to ask such a simple question, but I can't find the answer after extensive searching the docs and the web. How do you remove a component from a list? For example say you have: lst-c(5,6,7,8,9) How do you remove, for example, the third component in the list? lst[[3]]]-NULL generates an error: Error: more elements supplied than there are to replace Also, how do you remove a row from a data frame? For example, say you have: lst1-c(1,2,3,4,5) lst2-c(6,7,8,9,10) frame-data.frame(lst1,lst2) How do you remove, for example, the second row of frame? Thanks, - Jason __ 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 and provide commented, minimal, self-contained, reproducible code. -- Henrique Dallazuanna Curitiba-ParanĂ¡ Brasil [[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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] remove component from list or data frame
On 2/8/2007 12:30 PM, Jason Horn wrote: Sorry to ask such a simple question, but I can't find the answer after extensive searching the docs and the web. How do you remove a component from a list? For example say you have: lst-c(5,6,7,8,9) In R jargon, that's a vector, not a list. How do you remove, for example, the third component in the list? lst[-3] will do it. lst[[3]]]-NULL generates an error: Error: more elements supplied than there are to replace The [[ index ]] syntax only works on true lists. Also, how do you remove a row from a data frame? For example, say you have: lst1-c(1,2,3,4,5) lst2-c(6,7,8,9,10) frame-data.frame(lst1,lst2) How do you remove, for example, the second row of frame? Same idea: frame - frame[-2, ] Duncan Murdoch Thanks, - Jason __ 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 and provide commented, minimal, self-contained, reproducible code. __ 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] remove component from list or data frame
Jason Horn wrote: Sorry to ask such a simple question, but I can't find the answer after extensive searching the docs and the web. How do you remove a component from a list? For example say you have: lst-c(5,6,7,8,9) How do you remove, for example, the third component in the list? Is the object lst really a list? Try is.list(lst) to check. To remove an element from a vector, use for example, lst[-3] lst[[3]]]-NULL generates an error: Error: more elements supplied than there are to replace If lst were actually a list, that command would work with the obvious syntax fix. So would lst[-3] though. Also, how do you remove a row from a data frame? For example, say you have: lst1-c(1,2,3,4,5) lst2-c(6,7,8,9,10) frame-data.frame(lst1,lst2) How do you remove, for example, the second row of frame? You use frame[-2, ] #remove second row, keep all columns. Thanks, - Jason __ 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 and provide commented, minimal, self-contained, reproducible code. __ 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] remove component from list or data frame
On 2/8/2007 1:09 PM, Duncan Murdoch wrote: On 2/8/2007 12:30 PM, Jason Horn wrote: Sorry to ask such a simple question, but I can't find the answer after extensive searching the docs and the web. How do you remove a component from a list? For example say you have: lst-c(5,6,7,8,9) In R jargon, that's a vector, not a list. How do you remove, for example, the third component in the list? lst[-3] will do it. lst[[3]]]-NULL generates an error: Error: more elements supplied than there are to replace The [[ index ]] syntax only works on true lists. Sigh. This is just my wishful thinking. It works on numeric vectors too, sometimes. Just not here. Duncan Murdoch Also, how do you remove a row from a data frame? For example, say you have: lst1-c(1,2,3,4,5) lst2-c(6,7,8,9,10) frame-data.frame(lst1,lst2) How do you remove, for example, the second row of frame? Same idea: frame - frame[-2, ] Duncan Murdoch Thanks, - Jason __ 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 and provide commented, minimal, self-contained, reproducible code. __ 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 and provide commented, minimal, self-contained, reproducible code. __ 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 and provide commented, minimal, self-contained, reproducible code.