Re: [R] Extract from data.frame

2015-09-22 Thread Nico Gutierrez
Thank you all! n On Mon, Sep 21, 2015 at 10:00 PM, Bert Gunter wrote: > No. > > > On Mon, Sep 21, 2015 at 10:58 AM, John McKown > wrote: > > On Mon, Sep 21, 2015 at 9:52 AM, Nico Gutierrez < > nico.gutierr...@gmail.com> > > wrote: > > > >> Hi All, > >> > >> I need to do the following operation

Re: [R] extract from data.frame (indexing)

2015-09-21 Thread John Kane
And the action is? John Kane Kingston ON Canada > -Original Message- > From: nico.gutierr...@gmail.com > Sent: Mon, 21 Sep 2015 16:48:45 +0200 > To: r-help@r-project.org > Subject: [R] extract from data.frame (indexing) > > Hi All, > > I need to

Re: [R] Extract from data.frame

2015-09-21 Thread Frank Schwidom
better ( if year is an vector of more than 1 element): df[ df$Year %in% outer(as.numeric( as.character( year)), -1:1, FUN='+'), ] Year Amount 2 2002120 3 2003175 4 2004160 On Mon, Sep 21, 2015 at 10:49:34PM +0200, Frank Schwidom wrote: > > year <- df$Year[ which.max( df$Amount)]

Re: [R] Extract from data.frame

2015-09-21 Thread Frank Schwidom
year <- df$Year[ which.max( df$Amount)] df[ df$Year %in% (as.numeric( as.character( year)) + -1:1), ] Year Amount 2 2002120 3 2003175 4 2004160 On Mon, Sep 21, 2015 at 04:52:46PM +0200, Nico Gutierrez wrote: > Hi All, > > I need to do the following operation from data.frame: > >

Re: [R] Extract from data.frame

2015-09-21 Thread Bert Gunter
No. On Mon, Sep 21, 2015 at 10:58 AM, John McKown wrote: > On Mon, Sep 21, 2015 at 9:52 AM, Nico Gutierrez > wrote: > >> Hi All, >> >> I need to do the following operation from data.frame: >> >> df <- data.frame(Year = c("2001", "2002", "2003", "2004", "2005", "2006", >> "2007"), Amount = c(150

Re: [R] Extract from data.frame

2015-09-21 Thread Bert Gunter
Note the following problems: 1. " max_row <- (1:nrow(df))[which.max(df$Amount)]" This is a bit silly. max_row <- which.max(df$Amount) will do. See ?which.max 2. What happens if the max is the first or last row? e.g. > dat <- data.frame(a=runif(5),b=1:5) > max_row<- which.max(dat$b) > mean(d

Re: [R] Extract from data.frame

2015-09-21 Thread John McKown
On Mon, Sep 21, 2015 at 9:52 AM, Nico Gutierrez wrote: > Hi All, > > I need to do the following operation from data.frame: > > df <- data.frame(Year = c("2001", "2002", "2003", "2004", "2005", "2006", > "2007"), Amount = c(150, 120, 175, 160, 120, 105, 135)) > df[which.max(df$Amount),] #to extra

Re: [R] Extract from data.frame

2015-09-21 Thread Mark Sharp
Nico, I expect there are many better ways to do this, but this does work: max_row <- (1:nrow(df))[which.max(df$Amount)] mean(df$Amount[max_row + c(-1, 0, 1)]) > max_row <- (1:nrow(df))[which.max(df$Amount)] > mean(df$Amount[max_row + c(-1, 0, 1)]) [1] 151.6667 R. Mark Sharp, Ph.D. Director of P

[R] Extract from data.frame

2015-09-21 Thread Nico Gutierrez
Hi All, I need to do the following operation from data.frame: df <- data.frame(Year = c("2001", "2002", "2003", "2004", "2005", "2006", "2007"), Amount = c(150, 120, 175, 160, 120, 105, 135)) df[which.max(df$Amount),] #to extract row with max Amount. Now I need to do 3 years average around the

[R] extract from data.frame (indexing)

2015-09-21 Thread Nico Gutierrez
Hi All, I need to do the following operation: Year Amount Amount.1 1 2001150 150 2 2002120 120 3 2003175 175 4 2004160 160 5 2005120 120 6 2006105 105 7 2007135 135 [[alternative HTML version deleted]] ___