在R中,根据另一列的值计算一列的值。

3
使用数据框,我想创建一个新的数据框,其中包含Zip、Name和一个名为Count的列,该列将包括每个Zip中Name的数量。
Zip<-c("123245","12345","123245","123456","123456","12345")
Name<-c("Bob","Bob","Bob","Jack","Jack","Mary"),
df<-data.frame(Zip,Name,Name2)

library(dplyr)
df %>%
  group_by(Zip) %>%
  mutate(Name = cumsum(Name))

预期结果

Zip Name Count
1 123245  Bob     2
2  12345  Bob     1
3  12345 Mary     1
4 123456 Jack     2

1
你能展示一下期望的输出吗?不清楚你想要聚合计数、累计计数还是唯一计数。 - Ritchie Sacramento
我添加了exp输出。 - firmo23
1
当他们不是连续的时候,为什么在zip 123245中Bob有计数2,然后另一行相同的zip和计数1?我仍然很难理解你想要什么。 - dash2
there was typo i edited - firmo23
3个回答

5
我们可以使用`count`函数的`name`参数。
`count`函数本质上是对`group_by`和`summarise`的总结:
library(dplyr)
df %>% 
  count(Zip, Name, name= "Count")

     Zip Name Count
1 123245  Bob     2
2  12345  Bob     1
3  12345 Mary     1
4 123456 Jack     2

4

这解决了你的问题吗?

Zip<-c("123245","12345","123245","123456","123456","12345")
Name<-c("Bob","Bob","Bob","Jack","Jack","Mary")
df<-data.frame(Zip,Name)

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
df %>%
  group_by(Zip, Name) %>%
  summarise(Count = n())
#> `summarise()` has grouped output by 'Zip'. You can override using the `.groups` argument.
#> # A tibble: 4 × 3
#> # Groups:   Zip [3]
#>   Zip    Name  Count
#>   <chr>  <chr> <int>
#> 1 123245 Bob       2
#> 2 12345  Bob       1
#> 3 12345  Mary      1
#> 4 123456 Jack      2

本文创建于2021年12月22日,使用reprex包(v2.0.1)

--

快速速度基准测试:
library(tidyverse)
library(microbenchmark)

Zip<-c("123245","12345","123245","123456","123456","12345")
Name<-c("Bob","Bob","Bob","Jack","Jack","Mary")
df<-data.frame(Zip,Name)

JM <- function(df){
  df %>%
  group_by(Zip, Name) %>%
  summarise(Count = n())
}
JM(df)
#> `summarise()` has grouped output by 'Zip'. You can override using the `.groups` argument.
#> # A tibble: 4 × 3
#> # Groups:   Zip [3]
#>   Zip    Name  Count
#>   <chr>  <chr> <int>
#> 1 123245 Bob       2
#> 2 12345  Bob       1
#> 3 12345  Mary      1
#> 4 123456 Jack      2

TarJae <- function(df){
  df %>% 
    count(Zip, Name, name= "Count")
}

TIC <- function(df){
  aggregate(cbind(Count = Zip) ~ Zip + Name, df, length)
}
TIC(df)
#>      Zip Name Count
#> 1 123245  Bob     2
#> 2  12345  Bob     1
#> 3 123456 Jack     2
#> 4  12345 Mary     1

res <- microbenchmark(JM(df), TIC(df), TarJae(df))
autoplot(res)
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.

此内容由 reprex package (v2.0.1) 于2021年12月22日创建。


这不是预期的输出吗? - TarJae
1
看起来问题中的期望输出有一个错别字;如果有错别字,底部行应该被删除,有很多方法可以解决这个问题,例如你的 df %>% count(Zip, Name, name= "Count")(清晰简单,@TarJae,+1)。 - jared_mamrot
1
there was typo i edited - firmo23

3
使用`aggregte`的基本R选项
> aggregate(cbind(Count = Zip) ~ Zip + Name, df, length)
     Zip Name Count
1 123245  Bob     2
2  12345  Bob     1
3 123456 Jack     2
4  12345 Mary     1

1
不错的解决方案!它将比tidyverse方法快得多。 - jared_mamrot
@jared_mamrot 谢谢。我没有测试速度,但希望它与你预测的一样 :) - ThomasIsCoding
进行了快速的速度测试 - 使用示例数据集速度显著提高 :) - jared_mamrot
@jared_mamrot 有趣的基准测试!感谢您的努力。 - ThomasIsCoding

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接