英文:
Add a column of proportions by factor level in a data frame in R
问题
我有一个数据框,其中有一个"site"列,一个"species"列,一个"counts"列,以及其他列,我需要创建另一个列,该列根据"species"使用"counts"数据来计算比例。
site <- c(rep("site1",3), rep("site2",4), rep("site3",2))
species <- c("sp1","sp2","sp3","sp1","sp2","sp3","sp4","sp2","sp4")
counts <- c(4,10,22,1,13,23,4,3,1)
data <- cbind(site,species,counts)
# 计算每个物种的比例
data$proportions_by_species <- ave(data$counts, data$species, FUN=function(x) x/sum(x))
所得的数据框应该包含一个额外的列,该列包含根据"species"计算的比例,如下所示:
site species counts proportions_by_species
1 site1 sp1 4 0.80
2 site1 sp2 10 0.38
3 site1 sp3 22 0.49
4 site2 sp1 1 0.20
5 site2 sp2 13 0.50
6 site2 sp3 23 0.51
7 site2 sp4 4 0.80
8 site3 sp2 3 0.11
9 site3 sp4 1 0.20
英文:
I have a data frame with a a "site" column, a "species" column, a "counts" column, plus other columns, and I need to create another column with proportions by "species" using "counts" data.
site <- c(rep("site1",3), rep("site2",4), rep("site3",2))
species <- c("sp1","sp2","sp3","sp1","sp2","sp3","sp4","sp2","sp4")
counts <- c(4,10,22,1,13,23,4,3,1)
data <- cbind(site,species,counts)
site species counts ...
1 site1 sp1 4
2 site1 sp2 10
3 site1 sp3 22
4 site2 sp1 1
5 site2 sp2 13
6 site2 sp3 23
7 site2 sp4 4
8 site3 sp2 3
9 site3 sp4 1
My desired data frame should have and added column with proportions by "species", like this:
site species counts proportions_by_species ...
1 site1 sp1 4 0.80
2 site1 sp2 10 0.38
3 site1 sp3 22 0.49
4 site2 sp1 1 0.20
5 site2 sp2 13 0.50
6 site2 sp3 23 0.51
7 site2 sp4 4 0.80
8 site3 sp2 3 0.11
9 site3 sp4 1 0.20
答案1
得分: 1
library(dplyr)
site <- c(rep("site1", 3), rep("site2", 4), rep("site3", 2))
species <- c("sp1", "sp2", "sp3", "sp1", "sp2", "sp3", "sp4", "sp2", "sp4")
counts <- c(4, 10, 22, 1, 13, 23, 4, 3, 1)
data <- data.frame(site, species, counts)
data <- data %>%
group_by(species) %>%
mutate(proportions_by_species = prop.table(counts))
或者
data <- data %>%
group_by(species) %>%
mutate(proportions_by_species = counts/sum(counts))
英文:
library(dplyr)
site <- c(rep("site1",3), rep("site2",4), rep("site3",2))
species <- c("sp1","sp2","sp3","sp1","sp2","sp3","sp4","sp2","sp4")
counts <- c(4,10,22,1,13,23,4,3,1)
data <- data.frame(site,species,counts)
data <- data |>
group_by(species) |>
mutate(proportions_by_species = prop.table(counts))
Alternatively
data <- data |>
group_by(species) |>
mutate(proportions_by_species = counts/sum(counts))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论