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 ______________________________________________ R-help@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.