向 df 添加一列以计算另一列中某个值的出现次数

adding a column to df that counts occurrence of a value in another column

我想做的是按“位置”添加一列,计算整个列“id”中的 id 出现次数:

 id <- c(204850, 204850, 204850,312512,312512,452452,285421,758412,758412,758412)
places <- c("kitchen","kitchen","garden","salon","salon","salon","bathroom","garden","bathroom","garden")
 df <- data.frame(id, places)
 

    > df
           id   places
    1  204850  kitchen
    2  204850  kitchen
    3  204850   garden
    4  312512    salon
    5  312512    salon
    6  452452    salon
    7  285421 bathroom
    8  758412   garden
    9  758412 bathroom
    10 758412   garden

我看到的唯一选项是在 dplyr 中进行计数,但它会创建一个新的数据框。

输出应如下所示:

> df
       id   places id_occurrence
1  204850  kitchen             3
2  204850  kitchen             3
3  204850   garden             3
4  312512    salon             2
5  312512    salon             2
6  452452    salon             1
7  285421 bathroom             1
8  758412   garden             3
9  758412 bathroom             3
10 758412   garden             3

您可以使用以下解决方案:

library(dplyr)

df %>%
  group_by(id) %>%
  add_count(name = "id_occurrence")

# A tibble: 10 x 3
# Groups:   id [5]
       id places   id_occurrence
    <dbl> <chr>            <int>
 1 204850 kitchen              3
 2 204850 kitchen              3
 3 204850 garden               3
 4 312512 salon                2
 5 312512 salon                2
 6 452452 salon                1
 7 285421 bathroom             1
 8 758412 garden               3
 9 758412 bathroom             3
10 758412 garden               3

我们也可以在不分组的情况下做到这一点

library(dplyr)
df %>%
  add_count(id, name = 'id_occurence')