Removing rows w/ smaller value from data frame

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Removing rows w/ smaller value from data frame

ramoss
Hello,

I have a column called max_date in my data frame and I only want to keep the bigger values for the same activity.  How can I do that?

data frame:

activity    max_dt
A            2013-03-05
B             2013-03-28
A             2013-03-28
C             2013-03-28
B             2013-03-01

Thank you for your help
Reply | Threaded
Open this post in threaded view
|

Re: Removing rows w/ smaller value from data frame

PIKAL Petr
Hi

change max_dt do PISIX class and use standard comparison operator and use the result for selecting rows.

> s<-seq(c(ISOdate(2000,3,20)), by = "day", length.out = 10)
> s<s[5]
 [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
>

Regards
Petr


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of ramoss
> Sent: Thursday, May 23, 2013 4:24 PM
> To: [hidden email]
> Subject: [R] Removing rows w/ smaller value from data frame
>
> Hello,
>
> I have a column called max_date in my data frame and I only want to
> keep the bigger values for the same activity.  How can I do that?
>
> data frame:
>
> activity    max_dt
> A            2013-03-05
> B             2013-03-28
> A             2013-03-28
> C             2013-03-28
> B             2013-03-01
>
> Thank you for your help
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Removing-
> rows-w-smaller-value-from-data-frame-tp4667816.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Removing rows w/ smaller value from data frame

arun kirshna
In reply to this post by ramoss
Hi,
Try:
datNew<- read.table(text="
activity    max_dt
A            2013-03-05
B            2013-03-28
A            2013-03-28
C            2013-03-28
B            2013-03-01
",sep="",header=TRUE,stringsAsFactors=FALSE)
datNew$max_dt<- as.Date(datNew$max_dt)
 aggregate(max_dt~activity,data=datNew,max)
#  activity     max_dt
#1        A 2013-03-28
#2        B 2013-03-28
#3        C 2013-03-28
#or

library(plyr)
 ddply(datNew,.(activity),summarize, max_dt=max(max_dt))
#  activity     max_dt
#1        A 2013-03-28
#2        B 2013-03-28
#3        C 2013-03-28
#or
ddply(datNew,.(activity),summarize, max_dt=tail(sort(max_dt),1))
#  activity     max_dt
#1        A 2013-03-28
#2        B 2013-03-28
#3        C 2013-03-28


A.K.

----- Original Message -----
From: ramoss <[hidden email]>
To: [hidden email]
Cc:
Sent: Thursday, May 23, 2013 10:23 AM
Subject: [R] Removing rows w/ smaller value from data frame

Hello,

I have a column called max_date in my data frame and I only want to keep the
bigger values for the same activity.  How can I do that?

data frame:

activity    max_dt
A            2013-03-05
B             2013-03-28
A             2013-03-28
C             2013-03-28
B             2013-03-01

Thank you for your help



--
View this message in context: http://r.789695.n4.nabble.com/Removing-rows-w-smaller-value-from-data-frame-tp4667816.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Removing rows w/ smaller value from data frame

arun kirshna
>From your email, it seems like aggregate() is working.

Could you please provide the sessionInfo()?
My guess is that some other loaded library is masking the summarize().
For example, if I load
library(Hmisc)
#The following object is masked from ‘package:plyr’:
#
 #   is.discrete, summarize


ddply(datNew,.(activity),summarize, max_dt=max(max_dt)) #
#Error in is.list(by) : 'by' is missing
 ddply(datNew,.(activity),plyr::summarize,max_dt=max(max_dt))
#  activity     max_dt
#1        A 2013-03-28
#2        B 2013-03-28
#3        C 2013-03-28
A.K.


----- Original Message -----
From: "Mossadegh, Ramine N." <[hidden email]>
To: arun <[hidden email]>
Cc:
Sent: Thursday, May 23, 2013 10:44 AM
Subject: RE: [R] Removing rows w/ smaller value from data frame

Thank but I get : Error in is.list(by) : 'by' is missing
When I tried ddply(datNew,.(activity),summarize, max_dt=max(max_dt))

-----Original Message-----
From: arun [mailto:[hidden email]]
Sent: Thursday, May 23, 2013 10:40 AM
To: Mossadegh, Ramine N.
Cc: R help
Subject: Re: [R] Removing rows w/ smaller value from data frame

Hi,
Try:
datNew<- read.table(text="
activity    max_dt
A            2013-03-05
B            2013-03-28
A            2013-03-28
C            2013-03-28
B            2013-03-01
",sep="",header=TRUE,stringsAsFactors=FALSE)
datNew$max_dt<- as.Date(datNew$max_dt)
 aggregate(max_dt~activity,data=datNew,max)
#  activity     max_dt
#1        A 2013-03-28
#2        B 2013-03-28
#3        C 2013-03-28
#or

library(plyr)
 ddply(datNew,.(activity),summarize, max_dt=max(max_dt)) #  activity     max_dt
#1        A 2013-03-28
#2        B 2013-03-28
#3        C 2013-03-28
#or
ddply(datNew,.(activity),summarize, max_dt=tail(sort(max_dt),1)) #  activity     max_dt
#1        A 2013-03-28
#2        B 2013-03-28
#3        C 2013-03-28


A.K.

----- Original Message -----
From: ramoss <[hidden email]>
To: [hidden email]
Cc:
Sent: Thursday, May 23, 2013 10:23 AM
Subject: [R] Removing rows w/ smaller value from data frame

Hello,

I have a column called max_date in my data frame and I only want to keep the
bigger values for the same activity.  How can I do that?

data frame:

activity    max_dt
A            2013-03-05
B             2013-03-28
A             2013-03-28
C             2013-03-28
B             2013-03-01

Thank you for your help



--
View this message in context: http://r.789695.n4.nabble.com/Removing-rows-w-smaller-value-from-data-frame-tp4667816.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.


Confidentiality Notice:  This email, including attachments, may include non-public, proprietary, confidential or legally privileged information.  If you are not an intended recipient or an authorized agent of an intended recipient, you are hereby notified that any dissemination, distribution or copying of the information contained in or transmitted with this e-mail is unauthorized and strictly prohibited.  If you have received this email in error, please notify the sender by replying to this message and permanently delete this e-mail, its attachments, and any copies of it immediately.  You should not retain, copy or use this e-mail or any attachment for any purpose, nor disclose all or any part of the contents to any other person. Thank you

______________________________________________
[hidden email] 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.