An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131213/3e6b6c2d/attachment.pl>
dataframe manipulation
6 messages · Gang Chen, arun, Sarah Goslee +1 more
Hi, Try: ?d[match(unique(d$fac),d$fac),] A.K.
On Friday, December 13, 2013 4:17 PM, Gang Chen <gangchen6 at gmail.com> wrote:
Suppose I have a dataframe defined as ? ? L3 <- LETTERS[1:3] ? ? (d <- data.frame(cbind(x = 1, y = 1:10), fac = sample(L3, 10, replace = TRUE))) ? x? y fac 1? 1? 1? C 2? 1? 2? A 3? 1? 3? B 4? 1? 4? C 5? 1? 5? B 6? 1? 6? B 7? 1? 7? A 8? 1? 8? A 9? 1? 9? B 10 1 10? A I want to extract those rows that are the first occurrences for each level of factor 'fac', which are basically the first three rows above. How can I achieve that? The real dataframe is more complicated than the example above, and I can't simply list all the levels of factor 'fac' by exhaustibly listing all the levels like the following d[d$fac=='A' | d$fac=='B' | d$fac=='C', ] Thanks, Gang ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131213/d93ab9d7/attachment.pl>
What about: lapply(levels(d$fac), function(x)head(d[d$fac == x,], 1)) Thanks for the reproducible example. If you put set.seed(123) before the call to sample, then everyone who tries it will get the same data frame d. Sarah
On Fri, Dec 13, 2013 at 4:15 PM, Gang Chen <gangchen6 at gmail.com> wrote:
Suppose I have a dataframe defined as
L3 <- LETTERS[1:3]
(d <- data.frame(cbind(x = 1, y = 1:10), fac = sample(L3, 10, replace
= TRUE)))
x y fac
1 1 1 C
2 1 2 A
3 1 3 B
4 1 4 C
5 1 5 B
6 1 6 B
7 1 7 A
8 1 8 A
9 1 9 B
10 1 10 A
I want to extract those rows that are the first occurrences for each level
of factor 'fac', which are basically the first three rows above. How can I
achieve that? The real dataframe is more complicated than the example
above, and I can't simply list all the levels of factor 'fac' by
exhaustibly listing all the levels like the following
d[d$fac=='A' | d$fac=='B' | d$fac=='C', ]
Thanks,
Gang
Sarah Goslee http://www.functionaldiversity.org
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131213/c5921981/attachment.pl>
d[match(unique(d$fac),d$fac),]
The following does the same thing a little more directly (and quickly) d[ !duplicated(d$fac), ] Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Gang Chen Sent: Friday, December 13, 2013 1:35 PM To: arun Cc: R help Subject: Re: [R] dataframe manipulation Perfect! Thanks a lot, A.K! On Fri, Dec 13, 2013 at 4:21 PM, arun <smartpink111 at yahoo.com> wrote:
Hi,
Try:
d[match(unique(d$fac),d$fac),]
A.K.
On Friday, December 13, 2013 4:17 PM, Gang Chen <gangchen6 at gmail.com>
wrote:
Suppose I have a dataframe defined as
L3 <- LETTERS[1:3]
(d <- data.frame(cbind(x = 1, y = 1:10), fac = sample(L3, 10, replace
= TRUE)))
x y fac
1 1 1 C
2 1 2 A
3 1 3 B
4 1 4 C
5 1 5 B
6 1 6 B
7 1 7 A
8 1 8 A
9 1 9 B
10 1 10 A
I want to extract those rows that are the first occurrences for each level
of factor 'fac', which are basically the first three rows above. How can I
achieve that? The real dataframe is more complicated than the example
above, and I can't simply list all the levels of factor 'fac' by
exhaustibly listing all the levels like the following
d[d$fac=='A' | d$fac=='B' | d$fac=='C', ]
Thanks,
Gang
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.