我有一个data.frame c,它有两个列a和b。我想将列 a 和平均列 b 分组。 但效果不太好
这是我的数据框和代码
> c
a b
1 Red 60822
2 Red 343855
3 Blue 5510
4 Blue 5633
5 Red 299
6 Blue 42
7 Blue 140
8 Red 29
9 Blue 3935
10 Red 6546
11 Blue 17
12 Blue 26400
> c %>% as_tibble() %>% group_by(a) %>% summarise(dd = mean(b))
dd
1 37769
> class(c)
[1] "data.frame"
> str(c)
'data.frame': 12 obs. of 2 variables:
$ a: chr "Red" "Red" "Blue" "Blue" ...
$ b: num 60822 343855 5510 5633 299 ...
还有Rversion、Dplyr包版本:
> R.version
_
platform x86_64-w64-mingw32
arch x86_64
os mingw32
crt ucrt
system x86_64, mingw32
status
major 4
minor 2.1
year 2022
month 06
day 23
svn rev 82513
language R
version.string R version 4.2.1 (2022-06-23 ucrt)
nickname Funny-Looking Kid
> packageVersion("dplyr")
[1] ‘1.1.3’
更改为
cc = read.table(text = " a b
1 Red 60822
2 Red 343855
3 Blue 5510
4 Blue 5633
5 Red 299
6 Blue 42
7 Blue 140
8 Red 29
9 Blue 3935
10 Red 6546
11 Blue 17
12 Blue 26400", header = TRUE)
library(dplyr)
cc |>
summarise(dd = mean(b), .by = a)
给予
a dd
1 Red 82310.200
2 Blue 5953.857