Luca F
Luca F

Reputation: 145

How can I get the mean by groups in a list in R

I have a list like this:

Years sallary
1 121
12 4343
25 1341
23 12
15 325
2 574
4 5473
8 347
30 352
29 237
3 734
10 2469
11 1239
5 2456
20 231
6 9381
28 1284
13 1295
9 129
7 931
19 1293
27 1239
14 124
24 512
18 912
26 8321
17 12383
22 419
16 129
more than 30 years 12394
21 1239

Sorry for the huge list but I my list is even bigger and I have to make sure no one gives me the list hard-coded. What I want is a list with the mean of grouped years.


group1 <- c("less than 1 year", "1", "2", "3", "4", "5")
group2 <- c("6", "7", "8", "9", "10")
...

| Years | sallary | 
|:---- |:------:| 
| group1  | e.g. 1295    | 
| group2  | e.g. 12012   | 
| group3  | e.g. 8521    | 
| group4  | e.g. 2491    | 
| group5  | e.g. 12349   | 
| group6  | e.g. 1299    | 

I amm sorry for this list but otherwise it gives me an error if I do not put the table inside the code block... But that is the list I want.

Other answers doesn't help, because they calculate their means by same rows. But I have a strings.

Thanks for any pointers.

Upvotes: 1

Views: 158

Answers (1)

akrun
akrun

Reputation: 887118

We replace the 'Years' based on the group vectors. Get all the 'group' vectors into a list with mget, convert the list to two-column data.frame (stack), then do a join with the original data, replace the 'Years' column with the 'value', use that as grouping column and summarise the 'sallary' column

library(dplyr)
df2 <- stack(mget(ls(pattern = '^group\\d+$')))[2:1]
names(df2)[2] <- 'Years'
df1 %>% 
     left_join(df2, by = 'Years') %>%
     group_by(Years = ind) %>%
     summarise(sallary = mean(sallary, na.rm = TRUE))       
 

Upvotes: 2

Related Questions