library(tidyverse)
x <- c(0, 20, 30, 58)
n <- 100
df <- data_frame(x, n) %>%
distinct() %>%
filter(x >= 0 & x < n) %>%
arrange(x) %>%
bind_rows(data_frame(x = n)) %>%
mutate(lag_x = lag(x)) %>%
mutate(y = x - lag_x) %>%
filter(!is.na(y))
rep(seq_along(df$x), df$y)
上面的代码工作正常。当我尝试使最后一步进入所有中断时。我的语法有什么问题?我希望一切都成为长管。我认为这是不可能的,因为我需要将整个管道(上面的代码块)嵌套在rep()
调用中?正确吗?
library(tidyverse)
x <- c(0, 20, 30, 58)
n <- 100
df <- data_frame(x, n) %>%
distinct() %>%
filter(x >= 0 & x < n) %>%
arrange(x) %>%
bind_rows(data_frame(x = n)) %>%
mutate(lag_x = lag(x)) %>%
mutate(y = x - lag_x) %>%
filter(!is.na(y)) %>%
rep(seq_along(x), y) %>%
print()
function_list [i]中的错误:未找到对象'y'
答案 0 :(得分:5)
由于它不在mutate/summarise
函数之外,因此我们需要pull
或将其提取
data_frame(x, n) %>%
distinct() %>%
filter(x >= 0 & x < n) %>%
arrange(x) %>%
bind_rows(data_frame(x = n)) %>%
mutate(lag_x = lag(x)) %>%
mutate(y = x - lag_x) %>%
filter(!is.na(y)) %>%
{rep(seq_along(.$x), .$y)}
或者也可以写为
data_frame(x, n) %>%
distinct() %>%
filter(x >= 0 & x < n) %>%
arrange(x) %>%
bind_rows(data_frame(x = n)) %>%
mutate(lag_x = lag(x)) %>%
mutate(y = x - lag_x) %>%
filter(!is.na(y)) %>%
summarise(n = list(rep(row_number(), y))) %>%
pull(n)