英文:
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<-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")
> 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:
> 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 %>%
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论