R 如何创建数据摘要函数?

R 如何创建数据摘要函数?,r,function,arguments,summary,levels,R,Function,Arguments,Summary,Levels,我试图创建一个函数,它总结了几个向量,提示是 Write a function data_summary which takes three inputs:\ `dataset`: A data frame\ `vars`: A character vector whose elements are names of columns from dataset which the user wants summaries for\ `group.name`: A length one charac

我试图创建一个函数,它总结了几个向量,提示是

Write a function data_summary which takes three inputs:\
`dataset`: A data frame\
`vars`: A character vector whose elements are names of columns from dataset which the user wants summaries for\
`group.name`: A length one character vector which gives the name of the column from dataset which contains the factor which will be used as a grouping variable
\`var.names`: A character vector of the same length as vars which gives the names that the user would like used as the entries under “Variable” in the resulting output. This should be set equal to vars by default, so the default behavior is to use the column names from dataset.

The output of the function should be a data frame with the following structure:

Column names of the data frame will be:\
`Variable`\
`Missing`\
The `first` level of the factor group.name\
The `second` level of the factor group.name\
…\
The `kth` level of the factor group.name\
`p-value`
我已经设置了密码

data_summary <- function(dataset,vars,group.name,var.names) {
}

data\u summary您可以使用
dplyr
包来实现此功能。另外,我不知道您希望通过哪些函数汇总数据帧,所以我使用了
summary
函数从基本包返回的所有函数

我的数据:

> NewSKUMatrix
# A tibble: 268,918 x 4
   LagerID FilialID CSBID Price
     <int>    <int> <int> <dbl>
 1     233     2578  1005  38.3
 2     333     2543    NA  61.0
 3     334     2543    NA  15.0
 4     335     2543    NA  11.0
 5     337     2301    NA  71.0
 6     338     2031    NA  37.0
 7     338     2044    NA  35.0
 8     338     2054    NA  36.0
 9     338     2060    NA  37.0
10     338     2063    NA  36.0
# ... with 268,908 more rows
>NewSKUMatrix
#A tibble:268918x4
LagerID Filalid CSBID价格
1     233     2578  1005  38.3
2332543 NA 61.0
334 2543 NA 15.0
43352543 NA 11.0
53372301NA 71.0
6338 2031 NA 37.0
7338 2044 NA 35.0
83382054NA 36.0
93382060 NA 37.0
10338 2063 NA 36.0
# ... 还有268908行
功能:

数据汇总%
总结(
.vars=值,
.funs=列表(
最小值=最小值,
`第一个Qu.`=~分位数(x=,probs=0.25),
中位数=中位数,
平均数,
`第三个Qu.`=~分位数(x=,probs=0.75),
最大值=最大值
)
) %>%
将_重命名为(.vars=变量,
.funs=~name)
}
输出:

数据摘要(NewSKUMatrix、,
c(‘LagerID’),
c(“价格”),
c(“SKU”))
#A tibble:32454x7
SKU最小值'1st Qu.'中间值'3rd Qu.'最大值。
1    17  39.0      39.0   39.0  39.0      39.0  39.0
2    18 120.      120120121120140
3    21 289.      289289289289289
4    24  37.0      37.0   37.0  45.2      45.2  70.0
5    25  14.0      14.0   14.0  14.0      14.0  14.0
6    55  30.9      30.9   30.9  30.9      30.9  30.9
7   117  26.9      26.9   26.9  26.9      26.9  26.9
8   118  24.8      24.9   24.9  25.1      25.1  25.7
9   119  24.8      24.8   24.9  25.1      25.3  25.7
10   158 104.      108108107108108
# ... 还有32444行

这听起来好像作业不清楚,所以我建议你问问老师/教授。这并不是说我们不能在一个更明确的问题上提供帮助,但是。。。听起来在源代码上有点混乱。“编写一个函数数据摘要,它包含三个输入”…继续列出四个输入。经典我给我的教授发了电子邮件,她说:第四个输入与第二个输入相同。如果您想更改变量名,输入将进入:假设您想将变量sibsp更改为“兄弟姐妹/配偶”,则需要第四个输入,否则不必使用该参数。第1、第2、…第k个是组的级别。因此,例如,如果分组变量为“pclass”,那么对于分组变量的所有级别,都应该有mean&sds/比例(取决于x是连续的还是二进制的)。在这种情况下,它将是第一类、第二类和第三类。format_variable_summary函数将按照您想要的变量格式为您安排数据。它将从var_summary中提取信息并对其进行排列,当您在summary函数中调用它时,它将根据您的变量是什么生成该变量的格式化结果。如果您可以格式化结果而不使用该功能,请继续操作。
> NewSKUMatrix
# A tibble: 268,918 x 4
   LagerID FilialID CSBID Price
     <int>    <int> <int> <dbl>
 1     233     2578  1005  38.3
 2     333     2543    NA  61.0
 3     334     2543    NA  15.0
 4     335     2543    NA  11.0
 5     337     2301    NA  71.0
 6     338     2031    NA  37.0
 7     338     2044    NA  35.0
 8     338     2054    NA  36.0
 9     338     2060    NA  37.0
10     338     2063    NA  36.0
# ... with 268,908 more rows