标准化行中的名称并根据R中的相似行计算几何平均值
Standardize name in row and calculate the geometric mean based on similar row in R
我有一个数据table,我想在其中标准化"Sex"中的名称并计算基于每个组的几何平均值(如[=19=中的x、y和z) ]).
感谢您的帮助。
下面是 data.table.
library(data.table)
dt <- data.table(Group = c("x","x","x","y","z","z"), Sex = c("Man","Female","Feminine","Male","M","F"), Score = c(0,0.4,0.1,0.5,3,2.1))
谢谢。
这是你想要的吗?
geomean <- function(v) prod(v)**(1/length(v))
res <- tapply(dt$Score, dt$Group, geomean)
这给出了
> res
x y z
0.00000 0.50000 2.50998
或使用ave
创建新列
dt <- within(dt,gm <- ave(Score,Group,FUN = geomean))
> dt
Group Sex Score gm
1: x Man 0.0 0.00000
2: x Female 0.4 0.00000
3: x Feminine 0.1 0.00000
4: y Male 0.5 0.50000
5: z M 3.0 2.50998
6: z F 2.1 2.50998
编辑:
如果您想按 Group
和 Sex
对数据进行分组,请尝试以下操作
dt <- within(transform(dt,Sex = toupper(substr(Sex,1,1))),
gm <- ave(Score,Group,Sex,FUN = geomean))
因此
> dt
Group Sex Score gm
1: x M 0.0 0.0
2: x F 0.4 0.2
3: x F 0.1 0.2
4: y M 0.5 0.5
5: z M 3.0 3.0
6: z F 2.1 2.1
我有一个数据table,我想在其中标准化"Sex"中的名称并计算基于每个组的几何平均值(如[=19=中的x、y和z) ]).
感谢您的帮助。 下面是 data.table.
library(data.table)
dt <- data.table(Group = c("x","x","x","y","z","z"), Sex = c("Man","Female","Feminine","Male","M","F"), Score = c(0,0.4,0.1,0.5,3,2.1))
谢谢。
这是你想要的吗?
geomean <- function(v) prod(v)**(1/length(v))
res <- tapply(dt$Score, dt$Group, geomean)
这给出了
> res
x y z
0.00000 0.50000 2.50998
或使用ave
创建新列
dt <- within(dt,gm <- ave(Score,Group,FUN = geomean))
> dt
Group Sex Score gm
1: x Man 0.0 0.00000
2: x Female 0.4 0.00000
3: x Feminine 0.1 0.00000
4: y Male 0.5 0.50000
5: z M 3.0 2.50998
6: z F 2.1 2.50998
编辑:
如果您想按 Group
和 Sex
对数据进行分组,请尝试以下操作
dt <- within(transform(dt,Sex = toupper(substr(Sex,1,1))),
gm <- ave(Score,Group,Sex,FUN = geomean))
因此
> dt
Group Sex Score gm
1: x M 0.0 0.0
2: x F 0.4 0.2
3: x F 0.1 0.2
4: y M 0.5 0.5
5: z M 3.0 3.0
6: z F 2.1 2.1