计算一行中不同变量的数量

发布于 2025-01-15 03:56:08 字数 1057 浏览 3 评论 0原文

我有一个关于清理数据/检查观察结果的问题。由于隐私问题,我无法分享完整的代码或数据。我可以用一个例子来说明我想要实现的目标。假设这是我的数据集。

个体年份
1012018
1012019
1022019
1032019
1042017
1042018
1042019

假设我想计算这个数据集中不同个体的数量。那么,在本例中,它将是 4('101'、'102'、'103'、'104')。但是,我以一种非常广泛的方式来理解这一点。有没有一种简单的方法来检查总数据集有多少个人?

希望有人可以提供帮助:)我认为应该有一个简单的解决方案,但到目前为止谷歌搜索还没有帮助。

I have a question regarding cleaning data/checking observations. Due to privacy issues, I cannot share the full code or data. I can illustrate what I want to achieve with an example. Suppose this is my dataset.

IndividualYear
1012018
1012019
1022019
1032019
1042017
1042018
1042019

And suppose I want to count the number of different individuals in this dataset. Then, in this case, it would be 4 ('101', '102', '103', '104'). But, I have this in a very extended way. Is there an easy way to check how many individuals the total dataset has?

Hopefully, somebody can help :) I think there should be an easy solution to this, but googling hasn't helped so far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2025-01-22 03:56:08

这是您的示例数据:

data <- data.frame(Individual = c(101, 101, 102, 103, 104, 104, 104),
                   Year = c("2018", "2019", "2019", "2019", "2017", "2018", "2019"))

如下所示:

  Individual Year
1        101 2018
2        101 2019
3        102 2019
4        103 2019
5        104 2017
6        104 2018
7        104 2019

要计算列中唯一值的数量,您可以使用以下代码:

length(unique(data$Individual))

输出为:

[1] 4

本例中的输出为 4。

Here is your example data:

data <- data.frame(Individual = c(101, 101, 102, 103, 104, 104, 104),
                   Year = c("2018", "2019", "2019", "2019", "2017", "2018", "2019"))

Looks like this:

  Individual Year
1        101 2018
2        101 2019
3        102 2019
4        103 2019
5        104 2017
6        104 2018
7        104 2019

To count the number of unique values in a column, you can use the following code:

length(unique(data$Individual))

The output is:

[1] 4

The output is in this case 4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文