在R中,在数据框中按因子水平添加一列比例:

huangapple go评论69阅读模式
英文:

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 &lt;- c(rep(&quot;site1&quot;,3), rep(&quot;site2&quot;,4), rep(&quot;site3&quot;,2))
    species &lt;- c(&quot;sp1&quot;,&quot;sp2&quot;,&quot;sp3&quot;,&quot;sp1&quot;,&quot;sp2&quot;,&quot;sp3&quot;,&quot;sp4&quot;,&quot;sp2&quot;,&quot;sp4&quot;)
    counts &lt;- c(4,10,22,1,13,23,4,3,1)
    data &lt;- 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 &lt;- c(rep(&quot;site1&quot;,3), rep(&quot;site2&quot;,4), rep(&quot;site3&quot;,2))
species &lt;- c(&quot;sp1&quot;,&quot;sp2&quot;,&quot;sp3&quot;,&quot;sp1&quot;,&quot;sp2&quot;,&quot;sp3&quot;,&quot;sp4&quot;,&quot;sp2&quot;,&quot;sp4&quot;)
counts &lt;- c(4,10,22,1,13,23,4,3,1)
data &lt;- data.frame(site,species,counts)

data &lt;- data |&gt; 
  group_by(species) |&gt; 
  mutate(proportions_by_species = prop.table(counts))

Alternatively

data &lt;- data |&gt; 
  group_by(species) |&gt; 
  mutate(proportions_by_species = counts/sum(counts))

huangapple
  • 本文由 发表于 2023年7月17日 19:30:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定