Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中创建函数_R - Fatal编程技术网

在R中创建函数

在R中创建函数,r,R,我刚刚开始学习在R中编写函数。作为一个开始,我正在尝试复制summary函数,如下所示。但无法返回预期结果 summary_function <- function(df = as.data.frame(x)){ result <- summary(x) return(as.table(result)) } > summary_function(df = iris) ## below is the output I am getting Length Class

我刚刚开始学习在R中编写函数。作为一个开始,我正在尝试复制summary函数,如下所示。但无法返回预期结果

summary_function <- function(df = as.data.frame(x)){
  result <- summary(x)
  return(as.table(result))
}

> summary_function(df = iris)  ## below is the output I am getting
  Length Class  Mode   
a   4    -none- numeric
b  10    -none- numeric
c  20    -none- numeric
d 100    -none- numeric

summary\u函数尝试中的“as.table()”将摘要对象强制为不太清晰的格式

summary_function <- function(df = as.data.frame(x)){
  result <- summary(df)
  return(result)
}
将在RStudio的帮助面板中打开更多文档


在第二个问题中,“head()”中有一个“x”不合适,应该是“df”


前十行为什么要执行
函数(df=x)
?只需将其保留为
函数(x)
summary_function <- function(df = as.data.frame(x)){
  result <- summary(df)
  return(result)
}
summary_function <- function(df = as.data.frame(x)){
    summary(df)
}
?summary
first_ten_rows <- function(df = x){
  result <- head(df, n = 10)
  return(result)
}