0
votes

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!

1
aggregate(a ~ id + days, data=df, FUN=range) or with data.table: library("data.table"); setDT(df)[, .(minvalue=min(a), maxvalue=max(a)), by=.(id, days)] see stackoverflow.com/questions/3505701/…jogo

1 Answers

1
votes

You can use dplyr and group_by. For loops are very slow in R.

> library("dplyr")
> df %>% group_by(days, id) %>% summarise(minvalue = min(a), maxvalue = max(a))

   days    id minvalue maxvalue
  <dbl> <dbl>    <dbl>    <dbl>
1  1.00  1.10     1.00     3.00
2  1.00  1.20     1.00     1.00
3  2.00  1.10     1.00     1.00
4  2.00  1.20     5.00     5.00
5  3.00  1.10     3.00     3.00
6  3.00  1.20     1.00     2.00
7  4.00  1.10     1.00     1.00
8  4.00  1.20     2.00     5.00
9  5.00  1.10     3.00     5.00