如何使用R计算多个变量?
我有一个大型鸟类观测数据集。我想按组计数,即按不同类别观察到的物种:年份、季节和网格 例如,2017年观察到多少美国乌鸦AMCR?或者2017年在繁殖季节BB栏中观察到多少只美国知更鸟 下面是我的标题和第一行数据的示例: 数据头如何使用R计算多个变量?,r,variables,count,R,Variables,Count,我有一个大型鸟类观测数据集。我想按组计数,即按不同类别观察到的物种:年份、季节和网格 例如,2017年观察到多少美国乌鸦AMCR?或者2017年在繁殖季节BB栏中观察到多少只美国知更鸟 下面是我的标题和第一行数据的示例: 数据头 我尝试使用dplyr count_u和group_uby,但我认为我做得不对。请帮忙 听起来你在试图统计小组内的观察次数。这就是dplyr中count的设计目的。诀窍在于,你不需要一组人在它之前 下面是一些示例代码: 图书馆弹琴 数据风暴 按组计数% 要在其中计算观察值
我尝试使用dplyr count_u和group_uby,但我认为我做得不对。请帮忙 听起来你在试图统计小组内的观察次数。这就是dplyr中count的设计目的。诀窍在于,你不需要一组人在它之前 下面是一些示例代码: 图书馆弹琴 数据风暴 按组计数% 要在其中计算观察值的变量 年份、月份、状态 或者,如果您的原始数据中有一个名为Count的变量,并且您希望在每个组中对其进行汇总,则应改为使用summary with group_by 按集团划分的总额% 按年度、月份、状态划分的组%>% 压力在这里没有多大意义,但不管你想总结什么变量 SummaryCount=总压力
下面是使用dplyr的另一个解决方案。它与先前建议的类似;然而,我认为这可能更接近你想要做的事情。 要按年份、季节和网格计算观察物种的数量:
#Count number of species
df %>%
#Grouping variables
group_by(Year, Season, Grid) %>%
#Remove possible duplicates in the species column
distinct(Species) %>%
#Count number of species
count(name = "SpCount")
#Count number of birds per species
df %>%
#Grouping variables
group_by(Species, Year, Season, Grid) %>%
#Count number of birds per species
summarize(BirdCount = sum(Count))
按物种、年份、季节和网格计算观察到的鸟类数量:
#Count number of species
df %>%
#Grouping variables
group_by(Year, Season, Grid) %>%
#Remove possible duplicates in the species column
distinct(Species) %>%
#Count number of species
count(name = "SpCount")
#Count number of birds per species
df %>%
#Grouping variables
group_by(Species, Year, Season, Grid) %>%
#Count number of birds per species
summarize(BirdCount = sum(Count))
有一个可复制的例子会很有帮助。您可以在数据帧上执行dput函数,并将其粘贴到您的post.df%>%group\u byyear,season,grid%>%Summarien=n可能是您要查找的内容。这是否回答了您的问题?