R 计算数据帧中出现的因子数,然后仅在因子数小于或等于x时删除该行
我有一个数据框,其中R 计算数据帧中出现的因子数,然后仅在因子数小于或等于x时删除该行,r,dataframe,R,Dataframe,我有一个数据框,其中$type变量是factor df这是一个使用table()和%in%的基本R解决方案 df <- data.frame( type = c("a", "a", "b", "c", "b", "b", "c", "a", "d"), state = rep("Washington", 9) ) to_keep <- names(table(df$type))[table(df$type) >= 3] df <- df[df$type %in%
$type
变量是factor
df这是一个使用table()
和%in%
的基本R解决方案
df <- data.frame(
type = c("a", "a", "b", "c", "b", "b", "c", "a", "d"),
state = rep("Washington", 9)
)
to_keep <- names(table(df$type))[table(df$type) >= 3]
df <- df[df$type %in% to_keep, ]
df
#> type state
#> 1 a Washington
#> 2 a Washington
#> 3 b Washington
#> 5 b Washington
#> 6 b Washington
#> 8 a Washington
df 1 a华盛顿
#>华盛顿
#>华盛顿
#>5 b华盛顿
#>华盛顿
#>8 a华盛顿
这是一个基本的R解决方案,使用%
中的table()
和%
df <- data.frame(
type = c("a", "a", "b", "c", "b", "b", "c", "a", "d"),
state = rep("Washington", 9)
)
to_keep <- names(table(df$type))[table(df$type) >= 3]
df <- df[df$type %in% to_keep, ]
df
#> type state
#> 1 a Washington
#> 2 a Washington
#> 3 b Washington
#> 5 b Washington
#> 6 b Washington
#> 8 a Washington
df 1 a华盛顿
#>华盛顿
#>华盛顿
#>5 b华盛顿
#>华盛顿
#>8 a华盛顿
并使用dplyr
:
library(dplyr)
df %>%
group_by(type) %>%
filter(n()>2)
并使用dplyr
:
library(dplyr)
df %>%
group_by(type) %>%
filter(n()>2)