条件赋值 frame$col <- val with magrittr
Conditional Assignment frame$col <- val with magrittr
基于 Iris$Petal.Length 分配 Iris$column 的 Magrittr 语法是什么?没有 Magrittr 的示例:
df <- iris[47:56,]
df$val1 <- NA ## create column
df$val1[which(df$Petal.Length < 1.52)] <- "cake"
df$val1[which(df$Petal.Length > 1.55 & df$Petal.Length <=4.55)] <- "pie"
df$val1[which(df$Petal.Length > 4.55)] <- "apple"
head(df)
这导致:
Petal.Length Petal.Width Species val1
1.6 0.2 setosa pie
1.4 0.2 setosa cake
1.5 0.2 setosa cake
1.4 0.2 setosa cake
1.4 1.4. versicolor apple
我们可以用case_when
res <- df %>%
mutate(val1 = case_when(Petal.Length < 1.52 ~ 'cake',
Petal.Length > 1.55 & Petal.Length <= 4.55 ~ 'pie',
Petal.Length > 4.55 ~'apple'))
head(res, 5)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species val1
#1 5.1 3.8 1.6 0.2 setosa pie
#2 4.6 3.2 1.4 0.2 setosa cake
#3 5.3 3.7 1.5 0.2 setosa cake
#4 5.0 3.3 1.4 0.2 setosa cake
#5 7.0 3.2 4.7 1.4 versicolor apple
与您所写内容完全等效的 magrittr
语法是:
df %>% transform(val1 = NA) %$%
inset(.,Petal.Length < 1.52,"val1","cake") %$%
inset(.,Petal.Length > 1.55 & Petal.Length <= 4.55,"val1","pie") %$%
inset(.,Petal.Length > 4.55,"val1","apple")
或者对magrittr
的别名非常热心:
df %>% transform(val1 = NA) %$%
inset(.,Petal.Length %>% is_less_than(1.52),"val1","cake") %$%
inset(.,Petal.Length %>% is_greater_than(1.55) & Petal.Length %>%
is_weakly_less_than(4.55),"val1","pie") %$%
inset(.,Petal.Length %>% is_greater_than(4.55),"val1","apple")
还有一个变体:
df %>% transform(val1 = NA) %$%
inset(.,Petal.Length %>% is_less_than(1.52),"val1","cake") %$%
inset(.,Petal.Length %>% {is_greater_than(.,1.55) & is_weakly_less_than(.,4.55)},"val1","pie") %$%
inset(.,Petal.Length %>% is_greater_than(4.55),"val1","apple")
前两个在 base 中严格等同于此(管道除外!):
df %>% transform(val1 = NA) %$%
`[<-`(.,Petal.Length < 1.52,"val1","cake") %$%
`[<-`(.,Petal.Length > 1.55 & Petal.Length <= 4.55,"val1","pie") %$%
`[<-`(.,Petal.Length > 4.55,"val1","apple")
变体等同于:
df %>% transform(val1 = NA) %$%
`[<-`(.,Petal.Length < 1.52,"val1","cake") %$%
`[<-`(.,Petal.Length %>% {`>`(.,1.55) & `<=`(.,4.55)},"val1","pie") %$%
`[<-`(.,Petal.Length > 4.55,"val1","apple")
我使用 transform
因为它是一个 base
函数,而 mutate
是一个 dplyr
函数,但它们在这里的工作方式相同。
有关所有别名的定义,请参阅:?extract
基于 Iris$Petal.Length 分配 Iris$column 的 Magrittr 语法是什么?没有 Magrittr 的示例:
df <- iris[47:56,]
df$val1 <- NA ## create column
df$val1[which(df$Petal.Length < 1.52)] <- "cake"
df$val1[which(df$Petal.Length > 1.55 & df$Petal.Length <=4.55)] <- "pie"
df$val1[which(df$Petal.Length > 4.55)] <- "apple"
head(df)
这导致:
Petal.Length Petal.Width Species val1
1.6 0.2 setosa pie
1.4 0.2 setosa cake
1.5 0.2 setosa cake
1.4 0.2 setosa cake
1.4 1.4. versicolor apple
我们可以用case_when
res <- df %>%
mutate(val1 = case_when(Petal.Length < 1.52 ~ 'cake',
Petal.Length > 1.55 & Petal.Length <= 4.55 ~ 'pie',
Petal.Length > 4.55 ~'apple'))
head(res, 5)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species val1
#1 5.1 3.8 1.6 0.2 setosa pie
#2 4.6 3.2 1.4 0.2 setosa cake
#3 5.3 3.7 1.5 0.2 setosa cake
#4 5.0 3.3 1.4 0.2 setosa cake
#5 7.0 3.2 4.7 1.4 versicolor apple
与您所写内容完全等效的 magrittr
语法是:
df %>% transform(val1 = NA) %$%
inset(.,Petal.Length < 1.52,"val1","cake") %$%
inset(.,Petal.Length > 1.55 & Petal.Length <= 4.55,"val1","pie") %$%
inset(.,Petal.Length > 4.55,"val1","apple")
或者对magrittr
的别名非常热心:
df %>% transform(val1 = NA) %$%
inset(.,Petal.Length %>% is_less_than(1.52),"val1","cake") %$%
inset(.,Petal.Length %>% is_greater_than(1.55) & Petal.Length %>%
is_weakly_less_than(4.55),"val1","pie") %$%
inset(.,Petal.Length %>% is_greater_than(4.55),"val1","apple")
还有一个变体:
df %>% transform(val1 = NA) %$%
inset(.,Petal.Length %>% is_less_than(1.52),"val1","cake") %$%
inset(.,Petal.Length %>% {is_greater_than(.,1.55) & is_weakly_less_than(.,4.55)},"val1","pie") %$%
inset(.,Petal.Length %>% is_greater_than(4.55),"val1","apple")
前两个在 base 中严格等同于此(管道除外!):
df %>% transform(val1 = NA) %$%
`[<-`(.,Petal.Length < 1.52,"val1","cake") %$%
`[<-`(.,Petal.Length > 1.55 & Petal.Length <= 4.55,"val1","pie") %$%
`[<-`(.,Petal.Length > 4.55,"val1","apple")
变体等同于:
df %>% transform(val1 = NA) %$%
`[<-`(.,Petal.Length < 1.52,"val1","cake") %$%
`[<-`(.,Petal.Length %>% {`>`(.,1.55) & `<=`(.,4.55)},"val1","pie") %$%
`[<-`(.,Petal.Length > 4.55,"val1","apple")
我使用 transform
因为它是一个 base
函数,而 mutate
是一个 dplyr
函数,但它们在这里的工作方式相同。
有关所有别名的定义,请参阅:?extract