使用資料框,我想創建一個新的,其中包含 Zip、Name 和一個名為 Count 的列,該列將包含每個 Zip 的名稱計數。
Zip<-c("123245","12345","123245","123456","123456","12345")
Name<-c("Bob","Bob","Bob","Jack","Jack","Mary")
df<-data.frame(Zip,Name)
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
uj5u.com熱心網友回復:
我們可以使用 的name引數count。
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
uj5u.com熱心網友回復:
這能解決您的問題嗎?
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
由reprex 包(v2.0.1)于 2021 年 12 月 22 日創建
——
快速速度基準:
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 包(v2.0.1)于 2021 年 12 月 22 日創建
uj5u.com熱心網友回復:
使用基本 R 選項 aggregte
> 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/389579.html
標籤:r
