Here you go, but still seeing the same warning msg.
> name <- c('Mary','Sue','Peter','Mary','Mary','John','Sue','Peter','Peter','John',
+ 'John','John','Mary','Mary')
> date <- c('01/04/2018','03/02/2017','01/01/2019','24/04/2017',
+ '02/03/2019','31/05/2019','08/09/2019','17/12/2019',
+ '02/08/2017','10/11/2017','30/12/2017','18/02/2018',
+ '18/02/2018','18/10/2019')
>
> data <- data.frame(name,
+ date=lubridate::dmy(date))
>
> # Still have unique name as the row and list the trip 1,2,3,4,5 and
> # so on of each person and hv the avg trip n grand total at last column n row.
> data2 <- arrange(data,
+ name,date) %>%
+ group_by(name) %>%
+ mutate(order=row_number())
Error: row_number() should only be called in a data context
Run `rlang::last_error()` to see where the error occurred.
>
> times_df <- pivot_wider(data2,
+ id_cols=name,
+ names_from=order,
+ names_prefix = "time_",
+ values_from = "date")
Error in tbl_vars_dispatch(x) : object 'data2' not found
>
> times_df_lgl <- mutate_all(times_df,
+ as.logical)
Error in is_grouped_df(tbl) : object 'times_df' not found
> times_df_lgl$num <- rowSums(times_df_lgl[names(times_df_lgl)[-1]],na.rm = TRUE)
Error in is.data.frame(x) : object 'times_df_lgl' not found
> (avg_times<-mean(times_df_lgl$num,na.rm = TRUE))
Error in mean(times_df_lgl$num, na.rm = TRUE) :
object 'times_df_lgl' not found
> (sum_times<-sum(times_df_lgl$num,na.rm = TRUE))
Error: object 'times_df_lgl' not found
> times_df$num<-times_df_lgl$num
Error: object 'times_df_lgl' not found
> jam_it_together <- mutate_all(times_df,
+ as.character)
Error in is_grouped_df(tbl) : object 'times_df' not found
> jam_it_together <- bind_rows(jam_it_together,
+ mutate_all(data.frame(
+ name="Total",
+ time_1=sum_times,
+ time_2="Avg",
+ time_3=avg_times),as.character))
Error in dots_values(...) : object 'jam_it_together' not found
>
> # > jam_it_together
> # # A tibble: 5 x 7
> # # Groups: name [5]
> # name time_1 time_2 time_3 time_4 time_5 num
> # <chr> <chr> <chr> <chr> <chr> <chr> <chr>
> # 1 John 2017-11-10 2017-12-30 2018-02-18 2019-05-31 NA 4
> # 2 Mary 2017-04-24 2018-02-18 2018-04-01 2019-03-02 2019-10-18 5
> # 3 Peter 2017-08-02 2019-01-01 2019-12-17 NA NA 3
> # 4 Sue 2017-02-03 2019-09-08 NA NA NA 2
> # 5 Total 14 Avg 3.5 NA NA NA
>
> # The second table I want to know the lag days between trips. Lag is the day between trips.
> data3 <- mutate(data2,
+ lead_date=lead(date),
+ days_between = lead_date-date
+ )
Error in is.data.frame(.data) : object 'data2' not found
>
> data4 <- pivot_wider(data3 %>% filter(!is.na(days_between)),
+ id_cols=name,
+ names_from=order,
+ names_prefix = "lag_",
+ values_from = "days_between")
Error in eval(lhs, parent, parent) : object 'data3' not found
> #from here follow a similar approach to example 1
> library(tidyverse)
> library(lubridate)
> name <- c('Mary','Sue','Peter','Mary','Mary','John','Sue','Peter','Peter','John',
+ 'John','John','Mary','Mary')
> date <- c('01/04/2018','03/02/2017','01/01/2019','24/04/2017',
+ '02/03/2019','31/05/2019','08/09/2019','17/12/2019',
+ '02/08/2017','10/11/2017','30/12/2017','18/02/2018',
+ '18/02/2018','18/10/2019')
>
> data <- data.frame(name,
+ date=lubridate::dmy(date))
>
> # Still have unique name as the row and list the trip 1,2,3,4,5 and
> # so on of each person and hv the avg trip n grand total at last column n row.
> data2 <- arrange(data,
+ name,date) %>%
+ group_by(name) %>%
+ mutate(order=row_number())
Error: row_number() should only be called in a data context
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/rlang_error>
row_number() should only be called in a data context
Backtrace:
1. plyr::arrange(data, name, date)
1. dplyr::group_by(., name)
9. plyr::mutate(., order = row_number())
10. [ base::eval(...) ] with 1 more call
12. dplyr::row_number()
13. dplyr:::from_context("..group_size")
14. `%||%`(...)
Run `rlang::last_trace()` to see the full context.
> rlang::last_trace()
<error/rlang_error>
row_number() should only be called in a data context
Backtrace:
x
1. \-arrange(data, name, date) %>% group_by(name) %>% mutate(order = row_number())
2. +-base::withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
3. \-base::eval(quote(`_fseq`(`_lhs`)), env, env)
4. \-base::eval(quote(`_fseq`(`_lhs`)), env, env)
5. \-`_fseq`(`_lhs`)
6. \-magrittr::freduce(value, `_function_list`)
7. +-base::withVisible(function_list[[k]](value))
8. \-function_list[[k]](value)
9. \-plyr::mutate(., order = row_number())
10. \-base::eval(cols[[col]], .data, parent.frame())
11. \-base::eval(cols[[col]], .data, parent.frame())
12. \-dplyr::row_number()
13. \-dplyr:::from_context("..group_size")
14. \-`%||%`(...)