简体   繁体   English

如何应用具有多个 arguments 的 function 并创建 dataframe?

[英]How to apply a function with multiple arguments and create a dataframe?

I have a function that takes in two arguments fre(data, var) .我有一个 function 接收两个 arguments fre(data, var) I would like to apply this function to a few selected variables and create a dataframe by binding them by rows.我想将此 function 应用于一些选定的变量,并通过按行绑定它们来创建 dataframe。 I've done it the long way through this code.我已经通过这段代码做了很长的路要走。

Function : Function

fre <- function(.data, var) {
  var <- rlang::ensym(var)
  abc <- questionr::na.rm(.data[, rlang::as_string(var)])
  abc <- questionr::freq(abc)
  abc <- cbind(Label = rownames(abc), abc)
  abc <- questionr::rename.variable(abc, "n", "Frequency")
  abc <- questionr::rename.variable(abc, "%", "Percent")
  abc <- tidyr::separate(abc, Label, into = c("Value", "Label"), sep = "] ")
  row.names(abc) <- NULL
  abc <- abc %>% dplyr::mutate(Value = gsub("\\[|\\]", "", Value)) %>% 
    dplyr::select(Label, Value, Frequency, Percent) %>% 
    select(Label, Percent) 
  abc$Percent <- paste0(round(abc$Percent), "%")
  abc <- abc %>% 
    tidyr::pivot_wider(names_from = Label, values_from = Percent) 
  Label <- var_label(.data[[var]])
  Name <- deparse(substitute(var))
  abc <- cbind(Name, Label, abc)
  abc
}

Read example data:读取示例数据:

dat <- haven::read_spss("http://staff.bath.ac.uk/pssiw/stats2/SAQ.sav")

Run function with select variable names:使用 select 变量名运行 function:

r3 <- fre(dat, Q03)
r6 <- fre(dat, Q06)
r7 <- fre(dat, Q07)
r8 <- fre(dat, Q08)
r10 <- fre(dat, Q10)

Create dataset by binding rows:通过绑定行创建数据集:

rbind(r3, r6, r7, r8, r10)

Result:结果:

在此处输入图像描述

I'm looking for a way to simplify this code.我正在寻找一种简化此代码的方法。 I've tried using lapply but I'm getting different errors.我试过使用 lapply 但我得到了不同的错误。 For instance, when I try running lapply(list_var, fre) , I get this error: Error in is_string(x): argument "var" is missing, with no default .例如,当我尝试运行lapply(list_var, fre)时,出现此错误: Error in is_string(x): argument "var" is missing, with no default I know I need to pass multiple arguments to lapply, but I'm not sure how to do that with the function I created.我知道我需要将多个 arguments 传递给 lapply,但我不确定如何使用我创建的 function 来做到这一点。 Also, I'm looking for other ways apart from using lapply to quickly create a dataframe.另外,我正在寻找除了使用lapply快速创建 dataframe 之外的其他方法。

Change your function to accept string arguments:更改您的 function 以接受字符串 arguments:

fre <- function(.data, var) {
  abc <- questionr::na.rm(.data[, var])
  abc <- questionr::freq(abc)
  abc <- cbind(Label = rownames(abc), abc)
  abc <- questionr::rename.variable(abc, "n", "Frequency")
  abc <- questionr::rename.variable(abc, "%", "Percent")
  abc <- tidyr::separate(abc, Label, into = c("Value", "Label"), sep = "] ")
  row.names(abc) <- NULL
  abc <- abc %>% dplyr::mutate(Value = gsub("\\[|\\]", "", Value)) %>% 
    dplyr::select(Label, Value, Frequency, Percent) %>% 
    select(Label, Percent) 
  abc$Percent <- paste0(round(abc$Percent), "%")
  abc <- abc %>% 
    tidyr::pivot_wider(names_from = Label, values_from = Percent) 
  Label <- var_label(.data[[var]])
  Name <- var
  abc <- cbind(Name, Label, abc)
  abc
}

Then pass column names to fre function as string using lapply .然后使用lapply将列名作为字符串传递给fre function 。

cols <- c('Q03', 'Q06', 'Q07', 'Q08', 'Q10')
result <- do.call(rbind, lapply(cols, fre, .data = dat))
#Or a bit shorter
#result <- purrr::map_df(cols, fre, .data = dat))
result

#  Name                                       Label Strongly agree Agree Neither Disagree
#1  Q03               Standard deviations excite me            19%   26%     34%      17%
#2  Q06       I have little experience of computers            27%   44%     13%      10%
#3  Q07                       All computers hate me             7%   34%     26%      24%
#4  Q08       I have never been good at mathematics            15%   58%     19%       6%
#5  Q10 Computers are useful only for playing games            14%   57%     18%      10%
#  Strongly disagree
#1                3%
#2                6%
#3                8%
#4                3%
#5                2%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM