[R] Selecting first and last row for each date
arun
smartpink111 at yahoo.com
Wed Jun 4 20:28:26 CEST 2014
Hi,
May be this helps:
dat <- read.table(text="DATE Price
01.01.2010 2
01.01.2010 3
01.01.2010 2
01.01.2010 7
02.01.2010 3
02.01.2010 9
02.01.2010 0
03.01.2010 2
03.01.2010 4
03.01.2010 3
03.01.2010 6
03.01.2010 8",sep="",header=TRUE,stringsAsFactors=FALSE)
#If the data is ordered by "DATE"
indx <- with(dat, DATE[-1]!=DATE[-length(DATE)])
dat[sort(c(which(c(TRUE,indx)),which(c(indx,TRUE)))),]
#or
library(data.table)
dt1 <- data.table(dat, key="DATE")
dt1[,list(Price=c(Price[1],Price[.N])), by="DATE"]
A.K.
Hi Everybody.
My dataset is as follows:
DATE Price
01.01.2010 2
01.01.2010 3
01.01.2010 2
01.01.2010 7
02.01.2010 3
02.01.2010 9
02.01.2010 0
03.01.2010 2
03.01.2010 4
03.01.2010 3
03.01.2010 6
03.01.2010 8
I therefore have 2 columns.
I would like to extract the first and last row for each date. In other words, I would like to obtain:
DATE Price
01.01.2010 2
01.01.2010 7
02.01.2010 3
02.01.2010 0
03.01.2010 2
03.01.2010 8
Does someone know what is the necessary code to do this?
Thank you very much !
Greetings
More information about the R-help
mailing list