计算 r 中的独特因素

发布于 2024-11-05 14:23:08 字数 767 浏览 1 评论 0原文

我想知道在记录的每个出生日期出生的独特水坝的数量。我的数据框与此类似:

dam <- c("2A11","2A11","2A12","2A12","2A12","4D23","4D23","1X23")
bdate <- c("2009-10-01","2009-10-01","2009-10-01","2009-10-01",
           "2009-10-01","2009-10-03","2009-10-03","2009-10-03")
mydf <- data.frame(dam,bdate)
mydf
#    dam      bdate
# 1 2A11 2009-10-01
# 2 2A11 2009-10-01
# 3 2A12 2009-10-01
# 4 2A12 2009-10-01
# 5 2A12 2009-10-01
# 6 4D23 2009-10-03
# 7 4D23 2009-10-03
# 8 1X23 2009-10-03

我使用了aggregate(dam ~ bdate, data=mydf, FUN=length),但它计算了在特定日期出生的所有水坝

bdate dam
1 2009-10-01   5
2 2009-10-03   3

,相反,我需要有这样的事情:

mydf2
  bdate      dam
1 2009-10-01  2
2 2009-10-03  2

非常感谢您的帮助!

I would like to know the number of unique dams which gave birth on each of the birth dates recorded. My data frame is similar to this one:

dam <- c("2A11","2A11","2A12","2A12","2A12","4D23","4D23","1X23")
bdate <- c("2009-10-01","2009-10-01","2009-10-01","2009-10-01",
           "2009-10-01","2009-10-03","2009-10-03","2009-10-03")
mydf <- data.frame(dam,bdate)
mydf
#    dam      bdate
# 1 2A11 2009-10-01
# 2 2A11 2009-10-01
# 3 2A12 2009-10-01
# 4 2A12 2009-10-01
# 5 2A12 2009-10-01
# 6 4D23 2009-10-03
# 7 4D23 2009-10-03
# 8 1X23 2009-10-03

I used aggregate(dam ~ bdate, data=mydf, FUN=length) but it counts all the dams that gave birth on a particular date

bdate dam
1 2009-10-01   5
2 2009-10-03   3

Instead, I need to have something like this:

mydf2
  bdate      dam
1 2009-10-01  2
2 2009-10-03  2

Your help is very much appreciated!

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

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

发布评论

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

评论(4

凉薄对峙 2024-11-12 14:23:08

怎么样:

aggregate(dam ~ bdate, data=mydf, FUN=function(x) length(unique(x)))

What about:

aggregate(dam ~ bdate, data=mydf, FUN=function(x) length(unique(x)))
请恋爱 2024-11-12 14:23:08

您还可以先对数据运行 unique

aggregate(dam ~ bdate, data=unique(mydf[c("dam","date")]), FUN=length)

然后您也可以使用 table 而不是 aggregate,尽管输出略有不同。

> table(unique(mydf[c("dam","date")])$bdate)

2009-10-01 2009-10-03 
         2          2 

You could also run unique on the data first:

aggregate(dam ~ bdate, data=unique(mydf[c("dam","date")]), FUN=length)

Then you could also use table instead of aggregate, though the output is a little different.

> table(unique(mydf[c("dam","date")])$bdate)

2009-10-01 2009-10-03 
         2          2 
滥情哥ㄟ 2024-11-12 14:23:08

这只是如何思考问题以及如何解决问题的方法之一的示例。

split.mydf <- with(mydf, split(x = mydf, f = bdate)) #each list element has only one date.
# it's just a matter of counting unique dams
unique.mydf <- lapply(X = split.mydf, FUN = unique)
#and then count the number of unique elements
unilen.mydf <- lapply(unique.mydf, length)
#you can do these two last steps in one go like so
lapply(split.mydf, FUN = function(x) length(unique(x)))

as.data.frame(unlist(unilen.mydf)) #data.frame is just a special list, so this is water to your mill

           unlist(unilen.mydf)
2009-10-01                   2
2009-10-03                   2

This is just an example of how to think of the problem and one of the approaches on how to solve it.

split.mydf <- with(mydf, split(x = mydf, f = bdate)) #each list element has only one date.
# it's just a matter of counting unique dams
unique.mydf <- lapply(X = split.mydf, FUN = unique)
#and then count the number of unique elements
unilen.mydf <- lapply(unique.mydf, length)
#you can do these two last steps in one go like so
lapply(split.mydf, FUN = function(x) length(unique(x)))

as.data.frame(unlist(unilen.mydf)) #data.frame is just a special list, so this is water to your mill

           unlist(unilen.mydf)
2009-10-01                   2
2009-10-03                   2
擦肩而过的背影 2024-11-12 14:23:08

dplyr中,您可以使用n_distinct

library(tidyverse)
mydf %>%
  group_by(bdate) %>%
  summarize(dam = n_distinct(dam))

In dplyr you can use n_distinct :

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