R language notes | pipes: chaining

姑蘇中單選手發表於2020-11-29
df-sum <- df %>%
			mutate(bar = bar(foo)) %>%
			mutate(compare = bar > baz) %>%
			mutate(express = ifelse(compare, "greater than baz",baz )) %>%
			group_by(express) %>%
			summarise(n = n(), mean = mean(bar))

would be equal to

df_sum <- summarise(group_by(mutate(express = ifelse(mutate(compare = mutate(df, bar = bar(foo)) > baz), "greater than baz",baz ))), n = n(), mean = mean(bar))

pipe
%>% takes whatever is on the left of the pipe.
Check if there are the same variables on the left and right. If there are not, uses it as the first argument of the function on the right. Otherwise, rewrites the same variable.

相關文章