如何将两个输入传递给 R 中的 %>% 运算符
How to pass two inputs to %>% operator in R
最初我有这段代码,我试图使用 %>% 运算符来简化和提高可读性。
#Get Sunday's from 2018-01-01 till today
d = seq(as.Date("2018-01-01"),Sys.Date()+365,by='day')
HolidayList = data.frame(Holidays=d[weekdays(d)=='Sunday'])
#Get Alternate saturdays 2nd and 4th
d = seq(as.Date("2018-01-01"),Sys.Date()+365,by='day')
saturdayList = d[weekdays(d)=='Saturday']
altSaturdayList = lapply(split(saturdayList, format(saturdayList, "%Y-%m")), function(x)
na.omit(x[c(2,4)]))
altSaturdayList = as.data.frame(altSaturdayList)
altSaturdayList = gather(altSaturdayList)
altSaturdayList = subset(altSaturdayList,select = c(value))
HolidayList = rbind(HolidayList, setNames(altSaturdayList, "Holidays"))
HolidayList = rbind(HolidayList, setNames(as.data.frame(as.Date("2018-11-06")), "Holidays"))
HolidayList = unique(HolidayList)
HolidayList = arrange(HolidayList,Holidays)
rm(d)
rm(saturdayList)
rm(altSaturdayList)
我不想存储中间变量,因此我认为管道运算符是一个不错的选择,但我无法重现结果。
这是我试过的
HolidayList = saturdayList %>%
data.frame(lapply(split(saturdayList, format(saturdayList, "%Y-%m")), function(x)
na.omit(x[c(2,4)]))) %>%
subset(gather(altSaturdayList),select = c(value)) %>%
rbind(HolidayList, setNames(altSaturdayList, "Holidays")) %>%
rbind(HolidayList, setNames(as.data.frame(as.Date("2018-11-06")), "Holidays")) %>%
arrange(unique(HolidayList),Holidays)
Holidays
数据框有我要绑定的日期并添加新日期。
如何在不创建新变量的情况下获取每个步骤的输出并将其作为下一行的输入?
请解释步骤,以便我理解简化上述代码的过程。
tidyverse
中有专门为管道操作设计的功能。这些函数非常适合使用管道运算符而不是使用基本 R 运算符并尝试将它们放入管道中。您仍然可以在管道中使用基本 R 函数,但使用 tidyverse
工具更方便。
library(tidyverse)
temp <- map_dfr(split(saturdayList,format(saturdayList, "%Y-%m")), ~ .[c(2, 4)]) %>%
gather(key, Holidays) %>%
select(Holidays) %>%
bind_rows(HolidayList) %>%
add_row(Holidays = as.Date("2018-11-06")) %>%
unique() %>%
na.omit() %>%
arrange(Holidays)
temp
# Holidays
# <date>
# 1 2018-01-07
# 2 2018-01-13
# 3 2018-01-14
# 4 2018-01-21
# 5 2018-01-27
# 6 2018-01-28
# 7 2018-02-04
# 8 2018-02-10
# 9 2018-02-11
#10 2018-02-18
# … with 160 more rows
为了确认两者的输出是否相同,我们可以将 temp
转换为数据框,然后在最后一步与 HolidayList
进行比较。
identical(data.frame(temp), HolidayList)
#[1] TRUE
最初我有这段代码,我试图使用 %>% 运算符来简化和提高可读性。
#Get Sunday's from 2018-01-01 till today
d = seq(as.Date("2018-01-01"),Sys.Date()+365,by='day')
HolidayList = data.frame(Holidays=d[weekdays(d)=='Sunday'])
#Get Alternate saturdays 2nd and 4th
d = seq(as.Date("2018-01-01"),Sys.Date()+365,by='day')
saturdayList = d[weekdays(d)=='Saturday']
altSaturdayList = lapply(split(saturdayList, format(saturdayList, "%Y-%m")), function(x)
na.omit(x[c(2,4)]))
altSaturdayList = as.data.frame(altSaturdayList)
altSaturdayList = gather(altSaturdayList)
altSaturdayList = subset(altSaturdayList,select = c(value))
HolidayList = rbind(HolidayList, setNames(altSaturdayList, "Holidays"))
HolidayList = rbind(HolidayList, setNames(as.data.frame(as.Date("2018-11-06")), "Holidays"))
HolidayList = unique(HolidayList)
HolidayList = arrange(HolidayList,Holidays)
rm(d)
rm(saturdayList)
rm(altSaturdayList)
我不想存储中间变量,因此我认为管道运算符是一个不错的选择,但我无法重现结果。
这是我试过的
HolidayList = saturdayList %>%
data.frame(lapply(split(saturdayList, format(saturdayList, "%Y-%m")), function(x)
na.omit(x[c(2,4)]))) %>%
subset(gather(altSaturdayList),select = c(value)) %>%
rbind(HolidayList, setNames(altSaturdayList, "Holidays")) %>%
rbind(HolidayList, setNames(as.data.frame(as.Date("2018-11-06")), "Holidays")) %>%
arrange(unique(HolidayList),Holidays)
Holidays
数据框有我要绑定的日期并添加新日期。
如何在不创建新变量的情况下获取每个步骤的输出并将其作为下一行的输入?
请解释步骤,以便我理解简化上述代码的过程。
tidyverse
中有专门为管道操作设计的功能。这些函数非常适合使用管道运算符而不是使用基本 R 运算符并尝试将它们放入管道中。您仍然可以在管道中使用基本 R 函数,但使用 tidyverse
工具更方便。
library(tidyverse)
temp <- map_dfr(split(saturdayList,format(saturdayList, "%Y-%m")), ~ .[c(2, 4)]) %>%
gather(key, Holidays) %>%
select(Holidays) %>%
bind_rows(HolidayList) %>%
add_row(Holidays = as.Date("2018-11-06")) %>%
unique() %>%
na.omit() %>%
arrange(Holidays)
temp
# Holidays
# <date>
# 1 2018-01-07
# 2 2018-01-13
# 3 2018-01-14
# 4 2018-01-21
# 5 2018-01-27
# 6 2018-01-28
# 7 2018-02-04
# 8 2018-02-10
# 9 2018-02-11
#10 2018-02-18
# … with 160 more rows
为了确认两者的输出是否相同,我们可以将 temp
转换为数据框,然后在最后一步与 HolidayList
进行比较。
identical(data.frame(temp), HolidayList)
#[1] TRUE