Dplyr's mutate函數可以評估「鏈接」表達式,例如, library(dplyr)
data.frame(a = 1) %>%
mutate(b = a + 1, c = b * 2)
## a b c
## 1 1 2 4
這怎麼實現?在dplyr的源代碼快速瀏覽發現候補碼的基本結構: library(lazyeval)
library(rlang)
com
假設我想以編程方式過濾starwars數據框。這裏有一個簡單的例子,讓我根據家園和物種篩選: library(tidyverse)
# a function that allows the user to supply filters
filter_starwars <- function(filters) {
for (filter in filters) {
star
我在寫我的函數,並希望使用dplyr的filter()函數來選擇滿足條件的數據框的行。這是我的代碼: library(tidyverse)
df <-data.frame(x = sample(1:100, 50), y = rnorm(50), z = sample(1:100,50), w = sample(1:100, 50),
p = sample(1:100,50))
我想使用dplyr的programming magic,新的版本0.7.0,到3210兩列在一起。下面,我列出了一些我的嘗試。 df <- data_frame(x = c(1, 2, NA), y = c(2, NA, 3))
# What I want to do:
mutate(df, y = coalesce(x, y))
# Here's the expected outp
我可以選擇和安排單列toplists: iris %>%
select(Petal.Width, Species) %>%
arrange(desc(Petal.Width))
但我要爲整個數據框做到這一點。我用forloop來解決這個問題: features <- colnames(iris)
top <- data.frame()
for (i in 1:lengt