[英]create interval categories from a column including both numbers and characters in R
假设我有以下数据,我想添加 c 列,如果 b 列仅包含正值或负值,则我有类别<0, 0, 0-3, >3
否则 c 列中的类别将是 b 列包含的任何内容。
df <- data.frame(a= 1:14,
b= c(-1,-10,-2,0,0,2,1,4,10,12,6, "apple", "apple", "Orange"))
df
a b
1 1 -1
2 2 -10
3 3 -2
4 4 0
5 5 0
6 6 2
7 7 1
8 8 4
9 9 10
10 10 12
11 11 6
12 12 apple
13 13 apple
14 14 Orange
df2
a b c
1 1 -1 <0
2 2 -10 <0
3 3 -2 <0
4 4 0 0
5 5 0 0
6 6 2 0-3
7 7 1 0-3
8 8 4 >3
9 9 10 >3
10 10 12 >3
11 11 6 >3
12 12 apple apple
13 13 apple apple
14 14 Orange Orange
我正在尝试应用case_when
和cut
。 我得到了我需要的结果。 我将不胜感激任何帮助和提示。
df %>%
mutate(c = case_when( b %in% grepl("apple|orange", b) ~ b),
TRUE ~ cut(as.numeric(b),
breaks = c(-999, 0, 1, 4, 999),
labels = c("<0", "0", "0-3", ">3"),
right = F))
将数字从非数字中分离出来并单独进行可能会更好。 在base R
中,我们可以对每个子集进行两次赋值
i1 <- grepl("^-?[0-9]+$", df$b)
df$c[i1] <- as.character(cut(as.numeric(df$b[i1]),
breaks = c(-999, 0, 1, 4, 999), labels = c("<0", "0", "0-3", ">3"), right = FALSE))
df$c[!i1] <- df$b[!i1]
-输出
> df
a b c
1 1 -1 <0
2 2 -10 <0
3 3 -2 <0
4 4 0 0
5 5 0 0
6 6 2 0-3
7 7 1 0-3
8 8 4 >3
9 9 10 >3
10 10 12 >3
11 11 6 >3
12 12 apple apple
13 13 apple apple
14 14 Orange Orange
如果我们想使用dplyr
library(dplyr)
df %>%
mutate(c = coalesce(case_when( !grepl("apple|orange", b) ~ as.character(cut(as.numeric(b),
breaks = c(-999, 0, 1, 4, 999),
labels = c("<0", "0", "0-3", ">3"),
right = FALSE))), b))
-输出
a b c
1 1 -1 <0
2 2 -10 <0
3 3 -2 <0
4 4 0 0
5 5 0 0
6 6 2 0-3
7 7 1 0-3
8 8 4 >3
9 9 10 >3
10 10 12 >3
11 11 6 >3
12 12 apple apple
13 13 apple apple
14 14 Orange Orange
注意: case_when
或ifelse
将 function 应用于整个数据,因此当我们执行as.numeric
时,非数字元素被强制转换为NA
,因此第一个选项被覆盖。 相反,在case_when
之后使用replace
或coalesce
with b
column
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.