如何在R中从年龄类别计算数值年龄

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

How to calculate numeric age from age classes in R

问题

我有一个有关一系列年份的动物捕获数据集,如下所示:

Data <- cbind.data.frame(
  c("A", "B", "C", "A", "C", "D", "B", "C", "D"),
  c("Juvenile", "Adult", "Adult", "Adult", "Adult", "Juvenile", "Adult", "Adult", "Adult"),
  c(2010, 2010, 2010, 2011, 2011, 2011, 2012, 2012, 2012)
)

colnames(Data) <- c("ID", "Age_Class", "Year")

已知标识为"Juvenile"的个体年龄为2岁,标识为"Adult"的个体年龄至少为3岁,我想计算每个个体在不同年份的最小数值年龄。这将是期望的输出:

> Data
  ID Age_Class Year Age_Numeric
1  A  Juvenile 2010           2
2  B     Adult 2010           3
3  C     Adult 2010           3
4  A     Adult 2011           3
5  C     Adult 2011           4
6  D  Juvenile 2011           2
7  B     Adult 2012           5
8  C     Adult 2012           5
9  D     Adult 2012           3

如果有人可以帮助或指向先前类似问题的答案,将不胜感激!

英文:

I have a dataset on animal captures for a series of year, which looks like this

Data&lt;-cbind.data.frame(c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;A&quot;,&quot;C&quot;,&quot;D&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;),
                   c(&quot;Juvenile&quot;,&quot;Adult&quot;,&quot;Adult&quot;,&quot;Adult&quot;,&quot;Adult&quot;,&quot;Juvenile&quot;,&quot;Adult&quot;,&quot;Adult&quot;,&quot;Adult&quot;),
                   c(2010,2010,2010,2011,2011,2011,2012,2012,2012))
colnames(Data)&lt;-c(&quot;ID&quot;,&quot;Age_Class&quot;,&quot;Year&quot;)


 &gt; Data
  ID Age_Class Year
1  A  Juvenile 2010
2  B     Adult 2010
3  C     Adult 2010
4  A     Adult 2011
5  C     Adult 2011
6  D  Juvenile 2011
7  B     Adult 2012
8  C     Adult 2012
9  D     Adult 2012

Knowing that an individual identified as "Juvenile" is 2 years old, and an individual identified as "Adult" is at least 3 years old, I would like to calculate the minimun numeric age for each individual throughout the different years. This would be the desired output:

&gt; Data
  ID Age_Class Year Age_Numeric
1  A  Juvenile 2010           2
2  B     Adult 2010           3
3  C     Adult 2010           3
4  A     Adult 2011           3
5  C     Adult 2011           4
6  D  Juvenile 2011           2
7  B     Adult 2012           5
8  C     Adult 2012           5
9  D     Adult 2012           3

If someone can help out, or point me to a previous question on this, that would be much appreciated!

答案1

得分: 2

使用case_when函数,对于年龄为"Juvenile"的情况,将年龄设置为2,对于年龄为"Adult"的情况,将年龄设置为3,再加上给定年份与首次年份(作为"Adult"时的年份)之间的差值:

library(dplyr)
Data %>%
  group_by(ID) %>%
  mutate(Age = case_when(Age_Class == "Juvenile" ~ 2,
                         Age_Class == "Adult" ~ 3 + Year - first(Year[Age_Class == "Adult"]))) %>%
  ungroup()

这将产生以下结果:

  ID    Age_Class  Year   Age
1 A     Juvenile   2010     2
2 B     Adult      2010     3
3 C     Adult      2010     3
4 A     Adult      2011     3
5 C     Adult      2011     4
6 D     Juvenile   2011     2
7 B     Adult      2012     5
8 C     Adult      2012     5
9 D     Adult      2012     3
英文:

With case_when, set 2 for Juvenile, and for Adult 3 plus the difference between the given year and the first year as Adult:

library(dplyr)
Data %&gt;% 
  group_by(ID) %&gt;% 
  mutate(Age = case_when(Age_Class == &quot;Juvenile&quot; ~ 2,
                         Age_Class == &quot;Adult&quot; ~ 3 + Year - first(Year[Age_Class == &quot;Adult&quot;]))) %&gt;%
  ungroup()

  ID    Age_Class  Year   Age
1 A     Juvenile   2010     2
2 B     Adult      2010     3
3 C     Adult      2010     3
4 A     Adult      2011     3
5 C     Adult      2011     4
6 D     Juvenile   2011     2
7 B     Adult      2012     5
8 C     Adult      2012     5
9 D     Adult      2012     3

huangapple
  • 本文由 发表于 2023年2月24日 16:32:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554223.html
匿名

发表评论

匿名网友

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

确定