raw <- read_excel('discharge_data_1.xlsx')
df <- raw[,c(colnames(raw[1]),colnames(raw[2]))]
df %>% ptd_spc(value_field = "Actual pre 12pm discharges", date_field = "Date", improvement_direction = "decrease")`
I’m trying to understand why I can’t run using the below instead: df %>% ptd_spc(value_field = colnames(df[2]) , date_field = colnames(df[1]), improvement_direction = "decrease")
I get the error: value_field: 'colnames(df[2])' must be a valid column name in the data frame. Is there a way that I can create a variable for these fields which will be pulled directly from read_excel if the data changes (assume the columns of interest will always be the first two columns in the excel file)
I’m trying to pass the second column name of my excel file into the first argument value_field and the first column name of my excel file into the second argument date_field for the function ptd_spc(). Both colnames(df)[1] or colnames(df[1]) results in the error:
Error in check("value_field") :
value_field: 'colnames(df)[2]' must be a valid column name in the data frame.
e.g. df %>% ptd_spc(value_field = colnames(df[2]) , date_field = colnames(df[1]), improvement_direction = "decrease")
Oh indeed that wasn't the problem, sorry. To understand, you need to check the help of the function, I guess it's this one. The help for value_field and date_field says:
Field name can be specified using non-standard evaluation (i.e. no quotation marks).
So, that's a bit complicated, you can find details here, but the idea is you can use the special adverb .data as if it was the data frame itself:
library(tidyverse)
library(NHSRplotthedots)
df <- data.frame(`Actual pre 12pm discharges` = rnorm(4),
Date = as.Date(c("1jan1960", "2jan1960", "31mar1960", "30jul1960"), "%d%b%Y"),
check.names = FALSE)
x1 <- df %>%
ptd_spc(value_field = "Actual pre 12pm discharges",
date_field = "Date",
improvement_direction = "decrease")
#> Warning in ptd_add_short_group_warnings(.): Some groups have 'n < 12'
#> observations. These have trial limits, which will be revised with each
#> additional observation until 'n = fix_after_n_points' has been reached.
x2 <- df %>%
ptd_spc(value_field = .data[[colnames(df[1])]],
date_field = .data[[colnames(df[2])]],
improvement_direction = "decrease")
#> Warning in ptd_add_short_group_warnings(.): Some groups have 'n < 12'
#> observations. These have trial limits, which will be revised with each
#> additional observation until 'n = fix_after_n_points' has been reached.
waldo::compare(x1, x2)
#> ✔ No differences