dplyr链中的write.csv()

时间:2014-05-27 20:26:34

标签: r csv file-io dplyr

之前我遇到过这个问题,但只是在链外写了导出函数。有没有办法在write.csv链中包含dplyr语句?

library(dplyr)

data_set %>%
filter(Date == Sys.Date() - 1 | Date == Sys.Date()) %>%
write.csv('data_set_twodays.csv', row.names = F) %>%
filter(Date = Sys.Date()) %>%
write.csv('data_set_today.csv', row.names = F)
NULL

2 个答案:

答案 0 :(得分:16)

这在0.2版本中对我有用:

mtcars %>% filter(cyl == 4) %>% write.csv(.,file = "~/Desktop/piping.csv")

答案 1 :(得分:5)

你正在寻找T管道:

%T>%将运行该函数,但将输入作为输出传递

library(dplyr)
library(magrittr)

data_set %>%
filter(Date == Sys.Date() - 1 | Date == Sys.Date()) %T>%
write.csv('data_set_twodays.csv', row.names = F) %>%
filter(Date = Sys.Date()) %T>%
write.csv('data_set_today.csv', row.names = F) 

人们经常使用它来绘制中间体而不会破坏它们的管道!

来自文档:

rnorm(200) %>%
matrix(ncol = 2) %T>%
plot %>% # plot usually does not return anything. 
colSums