如何计算数据子集内唯一字符向量的数量 [英] How to count number of unique character vectors within a subset of data

查看:103
本文介绍了如何计算数据子集内唯一字符向量的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的数据集示例:

Here is an example of my dataset:

Dput -

(已删除)

我按照这个代码按月分组这个数据框:

And I am subsetting this data frame by month using this code:

simpleindoor.mean <- simple_trapindoors %>% group_by(month) %>% summarise(n=n(),mean = mean(bitingrate), stderror = std(bitingrate))

其中产生如下表格:

  |Month| n |   mean| stderror|
  |-----|---|-------|---------|                          
  |May  | 12|   0.25|     0.13|
  |June | 21|   0.53|     0.12|
  |July | 21|   0.53|     0.12|

我想做的是在同一功能中计算每个月内个人FAMILY_ID的数量,并将其添加为simpleindoor.mean的新列。

What I would like to do is calculate the number of individual FAMILY_ID's within each month in the same function, and add it as a new column to "simpleindoor.mean".

FAMILY_ID是一个字符向量。例如 6001-032。因此,如果在5月份有12个独特的FAMILY_ID,则新的数据列将在匹配May的行中显示12个。

FAMILY_ID is a character vector. E.g."6001-032". So if there were 12 unique FAMILY_ID's in May, the new column of data would show 12 in the row matching "May".

我已经看到了一些例子,您可以在其中查找字符向量的特定实例,但是我很难找到一个例子,您可以在其中计算出在特定组。我如何做?

I have seen examples where you look for specific instances of a character vector, but I struggled to find an example where you can count the instances of unique character vectors occurring within a particular group. How do I do this?

谢谢。

推荐答案

你需要 n_distinct

simple_trapindoors %>% group_by(month) %>% summarise(n=n(),mean = mean(bitingrate), stderror = std(bitingrate), 
                                                     UniqueFamilies = n_distinct(FAMILY_ID))

这篇关于如何计算数据子集内唯一字符向量的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆