How can i keep only latest entries?

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

How can i keep only latest entries?

Mat
Hello togehter,

i have a data.frame like this one:

    No. Date                 last change
1  1     2012-10-04     change settings
2  1     2012-10-20     bug fix
3  1     2012-11-05     final
4  2     2013-01-15    new task
5  2     2013-01-16    Bug fix
6  2     2013-01-17    final

now i want a new data.frame, who show me only the newest entries according to the No.
The solution look like this one:

   No. Date                last change
1  1     2012-11-05   final
2  2     2013-01-17   final

can anyone help me?

thanks.

Mat
Reply | Threaded
Open this post in threaded view
|

Re: How can i keep only latest entries?

arun kirshna
This post has NOT been accepted by the mailing list yet.
This post was updated on .
Hi,
YOu can do this in different ways:
dat1<-read.table(text="
    No.,Date,last change
 1,2012-10-04,change settings
  1,2012-10-20,bug fix
  1,2012-11-05,final
  2,2013-01-15,new task
  2,2013-01-16,Bug fix
  2,2013-01-17,final
",sep=",",header=TRUE,stringsAsFactors=FALSE)
 res<-dat1[grep("final",dat1$last.change),]
res
#  No.       Date last.change
#3   1 2012-11-05       final
#6   2 2013-01-17       final
do.call(rbind,lapply(split(dat1,dat1$No.),tail,1))
  #No.       Date last.change
#1   1 2012-11-05       final
#2   2 2013-01-17       final
library(plyr)
 ddply(dat1,.(No.),tail,1)
 # No.       Date last.change
#1   1 2012-11-05       final
#2   2 2013-01-17       final
aggregate(dat1,list(dat1$No.),tail,1)
 # Group.1 No.       Date last.change
#1       1   1 2012-11-05       final
#2       2   2 2013-01-17       final

A.K.
Reply | Threaded
Open this post in threaded view
|

Re: How can i keep only latest entries?

Rui Barradas
In reply to this post by Mat
Hello,

Try the following.
(I've named your data.frame 'dat')

do.call(rbind, lapply(split(dat, dat$`No.`), tail, 1))


Hope this helps,

Rui Barradas

Em 17-01-2013 10:50, Mat escreveu:

> Hello togehter,
>
> i have a data.frame like this one:
>
>      No. Date                 last change
> 1  1     2012-10-04     change settings
> 2  1     2012-10-20     bug fix
> 3  1     2012-11-05     final
> 4  2     2013-01-15    new task
> 5  2     2013-01-16    Bug fix
> 6  2     2013-01-17    final
>
> now i want a new data.frame, who show me only the newest entries according
> to the No.
> The solution look like this one:
>
>     No. Date                last change
> 1  1     2012-11-05   final
> 2  2     2013-01-17   final
>
> can anyone help me?
>
> thanks.
>
> Mat
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-can-i-keep-only-latest-entries-tp4655827.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: How can i keep only latest entries?

Patrick Connolly-4
In reply to this post by Mat
Quoting Mat <[hidden email]>:

> Hello togehter,
>
> i have a data.frame like this one:
>
>     No. Date                 last change
> 1  1     2012-10-04     change settings
> 2  1     2012-10-20     bug fix
> 3  1     2012-11-05     final
> 4  2     2013-01-15    new task
> 5  2     2013-01-16    Bug fix
> 6  2     2013-01-17    final
>

Calling your data frame, x and assuming the `last change` column is character,

x[x$`last change` == "final",]

will give what you want.  If `last change` is a factor column (likely),


x[as.character(x$`last change`) == "final",]


If you don't have spaces in your column names, you'd avoid using the
backticks.

HTH



> now i want a new data.frame, who show me only the newest entries according
> to the No.
> The solution look like this one:
>
>    No. Date                last change
> 1  1     2012-11-05   final
> 2  2     2013-01-17   final
>
> can anyone help me?
>
> thanks.
>
> Mat
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/How-can-i-keep-only-latest-entries-tp4655827.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.