Я хочу создать функцию, которая сэкономила бы мне много времени, печатая или копируя и вставляя. Я создаю эту функцию, чтобы потом использовать ее для другого анализа.
Функция
extractItemsForScales <- function(df=NULL, col_name_to_detect_scales=NULL, col_name_to_extract=NULL, scales=NULL, name_suffix=""){
if(is.null(df))
stop("Dataframe must not be null")
if(is.null(scales))
stop("Scales must not be null")
if(is.null(col_name_to_detect_scales))
stop("You must provide the column name to detect scales")
if(is.null(col_name_to_extract))
stop("You must provide the column name to extract the items from")
col_name_to_detect_scales <- enquo(col_name_to_detect_scales)
col_name_to_extract <- enquo(col_name_to_extract)
cat("Extracting items")
vars <- scales %>%
map(function(x){
df %>% filter(str_detect(!!col_name_to_detect_scales, x)) %>% pull(!!col_name_to_extract)
}) %>%
setNames(paste0(tolower(scales),name_suffix))
return(vars)
}
data
df <- structure(list(Scale = c("S01", "S05", "S05", "S01", "S01", "S11",
"S15", "S15", "S16", "S16", "S05", "S04", "S10", "S13", "S13",
"S05", "S10", "S09", "S07", "S07", "S06", "S06", "S06", "S07",
"S11", "S09", "S11", "S04", "S09", "S09", "S07", "S06", "S05",
"S05", "S06", "S05", "S01", "S01", "S01", "S12", "S01", "S02",
"S08", "S12", "S08", "S08", "S05", "S04"), SectionNo = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
Item = c("S1_23", "S1_29", "S1_36", "S1_41", "S1_46", "S1_6",
"S1_81", "S1_89", "S1_40", "S1_51", "S1_12", "S1_15", "S1_34",
"S1_44", "S1_50", "S1_78", "S1_73", "S1_77", "S1_31", "S1_59",
"S1_67", "S1_83", "S1_86", "S1_90", "S1_10", "S1_11", "S1_19",
"S1_26", "S1_30", "S1_45", "S1_49", "S1_61", "S1_62", "S1_91",
"S1_20", "S1_8", "S1_9", "S1_14", "S1_22", "S1_25", "S1_33",
"S1_53", "S1_54", "S1_92", "S1_63", "S1_64", "S1_71", "S1_47"
)), class = "data.frame", row.names = c(NA, -48L))
Желаемый выход
list(S01_48 = c("S1_23", "S1_41", "S1_46",
"S1_9", "S1_14", "S1_22", "S1_33"), S05_48 = c("S1_29",
"S1_36", "S1_12", "S1_78", "S1_62", "S1_91",
"S1_8", "S1_71"), S11_48 = c("S1_6", "S1_10",
"S1_19"), S15_48 = c("S1_81", "S1_89"), S16_48 = c("S1_40",
"S1_51"), S04_48 = c("S1_15", "S1_26", "S1_47"
), S10_48 = c("S1_34", "S1_73"), S13_48 = c("S1_44",
"S1_50"), S09_48 = c("S1_77", "S1_11", "S1_30",
"S1_45"), S07_48 = c("S1_31", "S1_59",
"S1_90", "S1_49"), S06_48 = c("S1_67", "S1_83",
"S1_86", "S1_61", "S1_20"), S12_48 = c("S1_25",
"S1_92"), S02_48 = "S1_53", S08_48 = c("S1_54",
"S1_63", "S1_64"))
вызов функции
Я бы вызвал функцию следующим образом:
scales <- structure(list(Scale = c("S01", "S05", "S11", "S15", "S16", "S04",
"S10", "S13", "S09", "S07", "S06", "S12", "S02", "S08")), class = "data.frame", row.names = c(NA,
-14L))
df %>% extractItemsForScales(col_name_to_detect_scales = Scale, col_name_to_extract = Item, scales = scales, name_suffix = "_48")
Но я продолжаю получать эту ошибку:
Error in extractItemsForScales(., col_name_to_detect_scales = Scale, col_name_to_extract = Item, :
object 'Item' not found
Почему я получаю эту ошибку? И как я могу это исправить?
ОБНОВЛЕНИЕ
При отладке кажется, что во время проверки is.null()
выдается ошибка col_name_to_extract
. Тем не менее, я не уверен, почему он ошибается в этой проверке, но не выше, для col_name_to_detect_scales