Re: [R] Subsetting depth profiles based on maximum depth by group with plyr

From: Peter Ehlers <ehlers_at_ucalgary.ca>
Date: Tue, 17 May 2011 05:57:27 -0700

On 2011-05-16 17:14, Sam Albers wrote:
> Hello,
>
> Apologies for a similar earlier post. I didn't include enough details in
> that one.
>
> I am having a little trouble subsetting some data based on a grouping
> variable. I am using an instrument that does depth profiles of a water
> column. The instrument records on the way down as well as the way up. So
> thanks to an off-list reply I can subset the data so that all data collected
> at the maximum depth and those collected on the way UP the water column are
> used and the data collected on the way DOWN through the water column are
> discarded. This is illustrated by the following:
>
> dat1<- data.frame(var=100*(0:10), depth=c(1:5,5,5:1))
> dat1[ seq_len(nrow(dat1))>= which.max(dat1$depth), ]
>
> However, I have data frame where I would like to perform this subset for
> several groups. My data.frame looks like the following:
>
> dat1<- data.frame(var=100*(0:10), depth=c(1:5,5,5:1))
> dat1$group<- "A"
> dat2<- data.frame(var=100*(0:10), depth=c(1:5,7,5:1))
> dat2$group<- "B"
> dat<- rbind(dat1,dat2)
>
> I thought I might be able to use the plyr package to do this but for some
> reason the following gives me almost the opposite of what I was hoping for:
>
>
> library(plyr)
> ddply(dat, .(group), function(.df) {
> .df[seq_len(nrow(.df)>= which.max(.df$depth)),]
> })
>
> Can anyone recommend a way to subset based on a grouping variable
> preferably?

I think that you just have a misplaced parenthesis:

      .df[seq_len(nrow(.df)>= which.max(.df$depth)),]

--> .df[seq_len(nrow(.df))>= which.max(.df$depth),]

(Thanks for providing a simple reproducible example.)

Peter Ehlers

>
> Thanks in advance.
>
> Sam
>
> [[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.



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. Received on Tue 17 May 2011 - 13:01:10 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 17 May 2011 - 13:30:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive