R 因子得到数字从到
R factors get numbers from to
我有数字列表,例如 0,1,0,1,2,3,0,5,6,7,5
w=factor(data,levels=c(a=0,b=1,2>))
在 2> 中,我想要高于 2 的关卡,但我不想拥有高于 3(0,1,高于 2) 的关卡。
您可以使用 pmin
:
来限制值
v <- c(0,1,0,1,2,3,0,5,6,7,5)
pmin(v, 2)
# [1] 0 1 0 1 2 2 0 2 2 2 2
这应该可以实现您的要求:
library("tidyverse")
c(0,1,0,1,2,3,0,5,6,7,5) %>%
as.character() %>%
as_factor() %>%
fct_other(keep = c("1", "2"))
#> [1] Other 1 Other 1 2 Other Other Other Other Other Other
#> Levels: 1 2 Other
我有数字列表,例如 0,1,0,1,2,3,0,5,6,7,5
w=factor(data,levels=c(a=0,b=1,2>))
在 2> 中,我想要高于 2 的关卡,但我不想拥有高于 3(0,1,高于 2) 的关卡。
您可以使用 pmin
:
v <- c(0,1,0,1,2,3,0,5,6,7,5)
pmin(v, 2)
# [1] 0 1 0 1 2 2 0 2 2 2 2
这应该可以实现您的要求:
library("tidyverse")
c(0,1,0,1,2,3,0,5,6,7,5) %>%
as.character() %>%
as_factor() %>%
fct_other(keep = c("1", "2"))
#> [1] Other 1 Other 1 2 Other Other Other Other Other Other
#> Levels: 1 2 Other