[Solved] Make columns in data frame equal to median, mean, etc.? (R) [closed]


Your dataframe:

df<-data.frame(x=c(1, 3 , 5), y=c(1, 1, 2), z=c(256, 5, 4))

Using dplyr/tidyr:

df1<-df%>%gather(var,val,x:z)%>%group_by(var)%>%summarise(max=max(val),min=min(val),avg=mean(val),median=median(val))

you can extend this to any number of summary stats.

# A tibble: 3 × 5
    var   max   min       avg median
  <chr> <dbl> <dbl>     <dbl>  <dbl>
1     x     5     1  3.000000      3
2     y     2     1  1.333333      1
3     z   256     4 88.333333      5

solved Make columns in data frame equal to median, mean, etc.? (R) [closed]