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)
The code above works fine. When I try and make the last step pipe into everything it breaks. What's off on my syntax? I want everything to be one long pipe. I think it's impossible because I'd need my whole pipe (the code chunk above) nested inside my rep()
call? Is that correct?
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()
Error in function_list[i] : object 'y' not found
As it is outside the mutate/summarise
functions, we need to pull
or extract it
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)}
Or it can also be written as
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.