给出如下所示的数据框df
text <- "
model,var,value
M1,a,12211
M1,b1,10.21
M1,b2,5.07
M1,c1,41.8
M1,c2,58.2
M1,d,1.6
M2,a,11922
M2,b1,15.6
M2,b2,8.9
M2,c1,38.1
M2,c2,61.9
M2,d,1.8
M2,a,13101
M2,b1,9.21
M2,b2,6.56
M2,c1,36.07
M2,c2,63.93
M2,d,1.75
"
df <- read.table(textConnection(text), sep=",", header = T)
我想通过dplyr var2
根据var
的值添加列mutate
。
基于以下逻辑。
如果var == 'b1'
或var == 'b2'
则All B
如果var == 'c1'
或var == 'c2'
则All C
别的var
我想按如下方式存储映射,并使用它来构建上述逻辑
mapping <- c("All B"= list(c('b1', 'b2')), "All C" = list(c('c1', 'c2')))
> mapping
$`All B`
[1] "b1" "b2"
$`All C`
[1] "c1" "c2"
预期输出
model var value var2
1 M1 a 12211.00 a
2 M1 b1 10.21 All B
3 M1 b2 5.07 All B
4 M1 c1 41.80 All C
5 M1 c2 58.20 All C
6 M1 d 1.60 d
7 M2 a 11922.00 a
8 M2 b1 15.60 All B
9 M2 b2 8.90 All B
10 M2 c1 38.10 All C
11 M2 c2 61.90 All C
12 M2 d 1.80 d
13 M2 a 13101.00 a
14 M2 b1 9.21 All B
15 M2 b2 6.56 All B
16 M2 c1 36.07 All C
17 M2 c2 63.93 All C
18 M2 d 1.75 d
我计划将dplyr与ifelse
一起使用,如下所示
df %>%
mutate(var2 = ifelse(# what should go here )
答案 0 :(得分:2)
以下是使用case_when
的示例解决方案(如评论中所示):
df %>%
mutate(
var = as.character(var),
var2 = case_when(
var == "b1" | var == "b2" ~ "All B",
var == "c1" | var == "c2" ~ "All C",
TRUE ~ var))
# model var value var2
#1 M1 a 12211.00 a
#2 M1 b1 10.21 All B
#3 M1 b2 5.07 All B
#4 M1 c1 41.80 All C
#5 M1 c2 58.20 All C
#6 M1 d 1.60 d
#7 M2 a 11922.00 a
#8 M2 b1 15.60 All B
#9 M2 b2 8.90 All B
#10 M2 c1 38.10 All C
#11 M2 c2 61.90 All C
#12 M2 d 1.80 d
#13 M2 a 13101.00 a
#14 M2 b1 9.21 All B
#15 M2 b2 6.56 All B
#16 M2 c1 36.07 All C
#17 M2 c2 63.93 All C
#18 M2 d 1.75 d
答案 1 :(得分:2)
顺便说一下,在基础R中有一个很少使用的levels<-
函数,它可以完成与所需结果几乎相同的操作。如果在右侧传递命名列表,则列表中的每个值都将替换为名称。因此,如果保留mapping
作为来源很重要,请尝试:
mapping <- c("B"= list(c('b1', 'b2')), "C" = list(c('c1', 'c2')))
df$var2 <- df$var
othval <- setdiff(df$var, unlist(mapping))
levels(df$var2) <- c(mapping, setNames(othval,othval))
# [1] a B B C C d a B B C C d a B B C C d
#Levels: B C a d
(这里的大部分内容都是针对mapping
未涵盖的案例)
答案 2 :(得分:1)
如果var
中存在mapping
,我们可以创建一个返回列表名称的函数,否则返回var
。我们可以使用rowwise()
为每一行执行此函数。
get_right_mapping <- function(var) {
names(which(sapply(mapping, function(x) var %in% x)))
}
library(dplyr)
df %>%
rowwise() %>%
mutate(var2 = ifelse(var %in% unlist(mapping), get_right_mapping(var), var))
# model var value var2
# <fct> <chr> <dbl> <chr>
# 1 M1 a 12211 a
# 2 M1 b1 10.2 All B
# 3 M1 b2 5.07 All B
# 4 M1 c1 41.8 All C
# 5 M1 c2 58.2 All C
# 6 M1 d 1.60 d
# 7 M2 a 11922 a
# 8 M2 b1 15.6 All B
# 9 M2 b2 8.90 All B
#10 M2 c1 38.1 All C
#11 M2 c2 61.9 All C
#12 M2 d 1.80 d
#13 M2 a 13101 a
#14 M2 b1 9.21 All B
#15 M2 b2 6.56 All B
#16 M2 c1 36.1 All C
#17 M2 c2 63.9 All C
#18 M2 d 1.75 d
数据强>
mapping <- c( "All A"= list(c('a1', 'a2')), "All B" = list(c('b1', 'b2')),
"All C" = list(c('c1','c2')))
df$var <- as.character(df$var)
答案 3 :(得分:0)
我简化了你的逻辑 - 如果我错了,请纠正我。
如果var
= b
或c
后跟数字,请分别替换All B
或All C
。
df %>%
mutate(var2 = gsub("^([bc])\\d+",
paste("All", "\\U\\1"),
var,
perl = TRUE))
model var value var2
1 M1 a 12211.00 a
2 M1 b1 10.21 All B
3 M1 b2 5.07 All B
4 M1 c1 41.80 All C
5 M1 c2 58.20 All C
6 M1 d 1.60 d
7 M2 a 11922.00 a
8 M2 b1 15.60 All B
9 M2 b2 8.90 All B
10 M2 c1 38.10 All C
11 M2 c2 61.90 All C
12 M2 d 1.80 d
13 M2 a 13101.00 a
14 M2 b1 9.21 All B
15 M2 b2 6.56 All B
16 M2 c1 36.07 All C
17 M2 c2 63.93 All C
18 M2 d 1.75 d
答案 4 :(得分:0)
如果您将babel -d ./dist
转换为mapping
和data.frame
,则表示您正确加入远离目标的值。
gather