基于Iris $ Petal.Length分配Iris $列的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
答案 0 :(得分:1)
我们可以使用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
答案 1 :(得分:1)
与您所写的完全相同的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")
两个第一个在基础上严格等同于此(管道除外):
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