[R] finding a min against an id in a column
arun
smartpink111 at yahoo.com
Sun Nov 10 06:11:56 CET 2013
Hi,
Try:
df <- data.frame(ID=c("ID1","ID1","ID2","ID3","ID1","ID2"), date=c("Mar03","Mar01","Mar05","Mar02","Mar02","Mar01"),stringsAsFactors=FALSE)
#as.Date(paste0(1,df[,2]),"%d%b%y") #assuming that year represents the digits
#as.Date(paste0(2001,df[,2]),"%Y%b%d")# if the digits represent days
#or
#strptime(df[,2],"%b%d")
#Either
aggregate(date~ID,data=df,function(x) format(min(as.Date(paste0(2001,x),"%Y%b%d")),"%b%d"))
#or
library(plyr)
ddply(df,.(ID),summarize, date=format(min(as.Date(paste0(2001,date),"%Y%b%d")),"%b%d"))
#or
df1 <- df[with(df,order(ID,as.Date(paste0(2001,date),"%Y%b%d"))),]
df1[c(1,diff(as.numeric(gsub("[^0-9]","",df1$ID)))) !=0,]
#or
df1[c(TRUE,df1$ID[-1] != df1$ID[-length(df1$ID)]),]
A.K.
I need some help to do something in r that is done using pivots in excel.
I have a data frame with IDs and dates against these ids. I need to get the minimum date against each id.
eg.
my datra frame is
df[,1]=c(ID1,ID1,ID2,ID3,ID1,ID2)
df[,2]=c(Mar03,Mar01. Mar 05,Mar02,Mar02,Mar01)
I need the result to show
ndf[,1] = ID1 ID2 ID3
ndf[,2] = Mar01 Mar 02 Mar 05
thanks...
More information about the R-help
mailing list