简体   繁体   中英

Loop for aggregated data frame in R

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM