如何根据另一列中的一个列中计数两个唯一值?

发布于 2025-02-12 18:38:00 字数 426 浏览 2 评论 0原文

如果有人能为此提供帮助,将不胜感激。我正在尝试将两个新变量添加到
我现有的df( *grade1sum和 *grade2sum)求和每个电影的等级(ET的分级三倍,因此 *级别的总数始终等于3),例如,

只有两个等级(1和2)。希望这是有道理的,因此,如果有人能提供帮助,将不胜感激(我想将它们添加到现有的DF中,因此目前我只有前三列)。

我正在添加我想要的DF看起来像图片,因为它无法正确格式: “在此处输入图像说明”

**其他人是否知道如何计算每部电影收到的每个年级的次数?

If anyone could help with this would be appreciated. I am trying to add two new variables to
my existing DF (*Grade1Sum and *Grade2Sum) summing each film's grade (ET got graded one three times so the *Grade1Sum total is always equal to 3 for example)

There are only two grades (1 and 2). Hopefully this makes sense, so if anyone can help, would be appreciated (I want to add them to an existing DF, thus currently I only have the first three columns).

I am adding what I want the DF to look like as a pic as it won't format correctly:enter image description here

**Additional does anyone know how to count the amount of times each grade was received per film?

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

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

发布评论

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

评论(1

与往事干杯 2025-02-19 18:38:00

这是一种做到这一点的方法,

df %>% 
   group_by(Film) %>% 
   mutate(Grade1Sum=sum(Grade[Grade==1]), Grade2Sum=sum(Grade[Grade==2]))

这是一种更灵活的方法

df %>%
   group_by(Film, Grade) %>%
   summarise(Sum=sum(Grade)) %>%
   pivot_wider(names_from=Grade, values_from=Sum,
               names_glue="Grade{Grade}{.value}") %>%
   right_join(., df)

Here is one way to do it

df %>% 
   group_by(Film) %>% 
   mutate(Grade1Sum=sum(Grade[Grade==1]), Grade2Sum=sum(Grade[Grade==2]))

Here is a more flexible way to do it

df %>%
   group_by(Film, Grade) %>%
   summarise(Sum=sum(Grade)) %>%
   pivot_wider(names_from=Grade, values_from=Sum,
               names_glue="Grade{Grade}{.value}") %>%
   right_join(., df)

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