用r聚合和总结字符对象
我有一个育种生产力数据集:
df1
# Nest.box Obs.type individual.number Clutch Chick.status
# 1 Nest1 Egg 1 First NA
# 2 Nest1 Egg 2 First NA
# 3 Nest1 Egg 3 First NA
# 4 Nest2 Egg 1 First NA
# 5 Nest2 Egg 2 First NA
# 6 Nest2 Egg 1 First NA
# 7 Nest1 Chick 1 First Dead
# 8 Nest1 Chick 2 First Fledged
# 9 Nest2 Chick 1 First Fledged
# 10 Nest2 Chick 2 First Fledged
# 11 Nest2 Chick 1 Second Fledged
# 12 Nest2 Chick 2 Second UNK
我想通过 Nest.box 和 Clutch 聚合来总结这些数据(按 Nest.box 显示“Fledged”的数量,按离合器)
想要的输出将是这样的:
output
# Nest.box Clutch Fledged
# 1 Nest1 First 1
# 2 Nest2 First 2
# 3 Nest2 Second 1
I have a breeding productivity dataset:
df1
# Nest.box Obs.type individual.number Clutch Chick.status
# 1 Nest1 Egg 1 First NA
# 2 Nest1 Egg 2 First NA
# 3 Nest1 Egg 3 First NA
# 4 Nest2 Egg 1 First NA
# 5 Nest2 Egg 2 First NA
# 6 Nest2 Egg 1 First NA
# 7 Nest1 Chick 1 First Dead
# 8 Nest1 Chick 2 First Fledged
# 9 Nest2 Chick 1 First Fledged
# 10 Nest2 Chick 2 First Fledged
# 11 Nest2 Chick 1 Second Fledged
# 12 Nest2 Chick 2 Second UNK
I want to summarise these data by aggregating by Nest.box and Clutch (shows the number of "Fledged" by nest.box, by clutch)
The wanted output would be something like this:
output
# Nest.box Clutch Fledged
# 1 Nest1 First 1
# 2 Nest2 First 2
# 3 Nest2 Second 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种选择是
过滤
,然后使用计数
:输出
数据
Another option is to
filter
then usecount
:Output
Data
这是一个潜在的解决方案:
在2022-04-04创建的 reprex package (v2.0.1.1) )
Here is a potential solution:
Created on 2022-04-04 by the reprex package (v2.0.1)
使用 data.table:
这会将
df
转换为 data.table (setDT(df)
),对Chick.status=='Fledged'
进行过滤>,并对按Nest.box
和Clutch
分组的行进行计数 (.N
)。Using data.table:
This converts
df
to a data.table (setDT(df)
), filters onChick.status=='Fledged'
, and counts (.N
) the rows grouped byNest.box
andClutch
.