Search
 
SCRIPT & CODE EXAMPLE
 

R

r mean by group

d <- read.table(text=
'Name     Month  Rate1     Rate2
Aira       1      12        23
Aira       2      18        73
Aira       3      19        45
Ben        1      53        19
Ben        2      22        87
Ben        3      19        45
Cat        1      22        87
Cat        2      67        43
Cat        3      45        32', header=TRUE)

aggregate(d[, 3:4], list(d$Name), mean)

  Group.1    Rate1    Rate2
1    Aira 16.33333 47.00000
2     Ben 31.33333 50.33333
3     Cat 44.66667 54.00000
Comment

group by mean in r

# several summary columns with arbitrary names
library(dplyr)
mtcars %>% 
  group_by(cyl, gear) %>%                            # multiple group columns
  summarise(sum_hp = sum(hp), mean_mpg = mean(mpg))  # multiple summary columns

# summarise all columns except grouping columns using "sum" 
mtcars %>% 
  group_by(cyl) %>% 
  summarise(across(everything(), sum))

# summarise all columns except grouping columns using "sum" and "mean"
mtcars %>% 
  group_by(cyl) %>% 
  summarise(across(everything(), list(mean = mean, sum = sum)))
Comment

PREVIOUS NEXT
Code Example
R :: r convert matrix to list of column vectors 
R :: merge multiple data frames in r 
R :: how to extract weekday from date in r 
R :: r box plots 
R :: data table R select several columns 
R :: pi in r 
R :: R find index where 
R :: how to read a vector input in r 
R :: how to create dictionary in R 
R :: r - extracting specific columns from a data frame 
R :: ggplot2 black and white theme 
R :: r variables 
R :: describe data in r 
R :: disable the y axis in plot r 
R :: regression in r with many variables 
R :: make gif r 
R :: truncate string in r 
R :: angular material number picker 
R :: Use regex to extract row in R (problem) 
R :: if else functionr 
R :: pairwise combinations r 
R :: colorblind-friendly palette in r 
R :: R grid all possibilites between two vectors 
R :: ggplot in R how to show information by hovering 
Rust :: check if a file exists rust 
Rust :: rust nested loop 
Rust :: rust into string 
Rust :: rust new vec 
Rust :: rust html parser 
Rust :: packet sniffing with rust 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =