I'm trying to loop through and return a min and max value calculation for a range of unique id's over a range of days. I tried to iterate through days then each unique value of days but it's not quite doing what I want it to I get a "list of 4" that has all of the iterations for the ID's but NA's for everything else.
days<-c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5)
id<-c(1.1,1.1,1.2,1.2,1.1,1.2,1.2,1.1,1.2,1.2,1.2,1.1,1.1,1.1,1.1)
a<-c(1,3,1,5,1,5,2,3,1,5,2,1,4,5,3)
df=data.frame(days,id,a)
newdf<-NULL
daylist<-unique(df$days)
idlist<-unique(df$id)
for (d in daylist) {
for (i in idlist) {
minvalue<-min(df$a)
maxvalue<-max(df$a)
newdf$minValue[i]<-minvalue
newdf$maxValue[i]<-maxvalue
newdf$day<-d
newdf$ID<-i
}
}
I want each row of the output data frame to consist of an ID, day, max, and min value of a for each day.
Thanks!
aggregate(a ~ id + days, data=df, FUN=range)
or withdata.table
:library("data.table"); setDT(df)[, .(minvalue=min(a), maxvalue=max(a)), by=.(id, days)]
see stackoverflow.com/questions/3505701/… – jogo