学习如何使用dplyr清理数据。我编写的代码清理了一些数据,然后比较了一个特定的兴趣变量(教育水平)与其他4个变量之间的关系。所以,我想要一个函数来清理数据,并返回一个tibble,它显示了4个变量与教育水平的比较。
我的原始代码没有问题,但是一旦我试图将我要做的概括成一个函数,dplyr包中的select()函数就不能工作了。它返回以下错误消息:
Error in `dplyr::select()`:
! object 'B7_b' not found
Run `rlang::last_error()` to see where the error occurred.其中,B7_b只是数据中的一个变量,我试图将其与教育水平(ppeducat)进行比较。
下面是没有问题的原始代码:
# preparing df of var_4 and doing some renaming
var_4 <- shed %>%
select(B7_b,ppeducat) %>%
filter_if(is.numeric, all_vars(. > 0)) %>%
mutate(B7_b = replace(B7_b, B7_b == 1, '1 - Poor')) %>%
mutate(B7_b = replace(B7_b, B7_b == 2, '2 - Only fair')) %>%
mutate(B7_b = replace(B7_b, B7_b == 3, '3 - Good')) %>%
mutate(B7_b = replace(B7_b, B7_b == 4, '4 - Excellent')) %>%
mutate(ppeducat = replace(ppeducat, ppeducat == 1, '1 - less than high school')) %>%
mutate(ppeducat = replace(ppeducat, ppeducat == 2, '2 - high school')) %>%
mutate(ppeducat = replace(ppeducat, ppeducat == 3, '3 - some college')) %>%
mutate(ppeducat = replace(ppeducat, ppeducat == 4, '4 - bachelors or better'))
var_4
ggplot(var_4, aes(x=ppeducat, fill=B7_b)) + geom_bar() + ggtitle("How would you rate the economic conditions of your country today?") + theme(plot.title=element_text(size=10))
# sort by rel. freq and rename the columns
var_4_clean <- var_4 %>%
group_by(ppeducat,B7_b) %>%summarize(n=n()) %>%mutate(Perc_Freq= n/sum(n)*100) %>%
rename(Agreement_Level = B7_b,
Education_Level = ppeducat,
n_responses = n,
Percent_Freq_of_Response = Perc_Freq)
# check the df
print(var_4_clean)我试图把它概括成一个函数.我去掉了重命名,因为并不是所有的4个变量在这方面都需要同样的调整。我不明白为什么这会改变什么。
新代码作为不起作用的函数:
summarize_var <- function(df, group_var) {
clean_var <- df %>%
dplyr::select(group_var,ppeducat) %>%
filter_if(is.numeric, all_vars(. > 0)) %>%
group_by(ppeducat,group_var) %>%summarize(n=n()) %>%mutate(Perc_Freq= n/sum(n)*100) %>%
rename(Agreement_Level = group_var,
Education_Level = ppeducat,
n_responses = n,
Percent_Freq_of_Response = Perc_Freq)
return(clean_var)
}
summarize_var(shed, B7_b)
shed梭是我正在工作的数据文件的名字。这是我在R中的第一个功能,所以请轻松一点!
发布于 2022-05-18 02:08:37
我想通了。如果在函数中使用tidyverse变量,则需要将它们括在{{}内。
https://stackoverflow.com/questions/72282412
复制相似问题