Home > Blockchain >  Loop for aggregated data frame in R
Loop for aggregated data frame in R

Time:05-26

I have a data frame with 58 columns labeled SD1 through to SD58 along with columns for date info (Date, Year, Month, Day).

I'm trying to find the date of the maximum value of each of the SD columns each year using the following code:

maxs<-aggregate(SD1~Year, data=SDtime, max)
SDMax<-merge(maxs,SDtime)

I only need the dates so I made a new df and relabeled the column as below:

SD1Max = subset(SDMax, select = c(Year, Date))
SD1Max %>%
  rename(
    SD1=Date
  )

I want to do the same thing for every SD column but I don't want to have to repeat these steps 58 times. Is there a way to loop the process?

CodePudding user response:

Assuming there are no ties (multiple days with where the variable reached its maximum) this probably does what you want:

library('tidyverse')

SDtime %>% 
  pivot_longer(
    cols = matches('^SD[0-9]{1,2}$')
  ) %>% 
  group_by(name) %>% 
  filter(value == max(value, na.rm = TRUE)) %>% 
  ungroup()

You might want to pivot_wider afterwards.

  • Related