Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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
如何使用dplyr计算列值的平均值?_R_Sql Server_Dplyr - Fatal编程技术网

如何使用dplyr计算列值的平均值?

如何使用dplyr计算列值的平均值?,r,sql-server,dplyr,R,Sql Server,Dplyr,我在R脚本中使用dplyr,SQL Server存储过程正在调用该脚本。我正在分析的查询输出如下所示: ALTER PROCEDURE [dbo].[spCodeMeans] -- Add the parameters for the stored procedure here @StudyID int, @StudyID_outer int OUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- i

我在R脚本中使用dplyr,SQL Server存储过程正在调用该脚本。我正在分析的查询输出如下所示:

ALTER PROCEDURE [dbo].[spCodeMeans]
-- Add the parameters for the stored procedure here
@StudyID int,
@StudyID_outer int OUT


AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
Declare @inquery nvarchar(max) = N'Select
        c.StudyID, c.RespID, c.ProductNumber, c.ProductSequence, c.BottomScaleValue, 
        c.BottomScaleAnchor, c.TopScaleValue, c.TopScaleAnchor, c.StudyDate,
        c.DayOfWeek, c.A, c.B, c.C, c.D, c.E, c.F,
        c.DependentVarYN, c.VariableAttributeID, c.VarAttributeName, c.[1] as c1, 
        c.[2] as c2, c.[3] as c3, c.[4] as c4, c.[5] as c5, c.[6] as c6, c.[7] as c7, c.[8] as c8
        from ClosedStudyResponses c
        --Sensory Value Attributes only for mean and standard deviation analytics.
        where VariableAttributeID = 1
        and c.StudyID = 21'
        ;
BEGIN TRY

        exec sp_execute_external_script
        @language = N'R',
        @script = N'
            OutputDataSet = data.frame(summary(InputDataSet))',
@input_data_1 = @inquery

END TRY

BEGIN CATCH
    THROW;
END CATCH
END

到目前为止,我的R脚本和TSQL查询是:

sqlQuerySensory <- "Select
        c.StudyID, c.RespID, c.ProductNumber, c.ProductSequence, c.BottomScaleValue, 
        c.BottomScaleAnchor, c.TopScaleValue, c.TopScaleAnchor, c.StudyDate,
        c.DayOfWeek, c.A, c.B, c.C, c.D, c.E, c.F,
        c.DependentVarYN, c.VariableAttributeID, c.VarAttributeName, c.[1] as c1, 
        c.[2] as c2, c.[3] as c3, c.[4] as c4, c.[5] as c5, c.[6] as c6, c.[7] as c7, c.[8] as c8
        from ClosedStudyResponses c
        --Sensory Value Attributes only for mean and standard deviation analytics.
        where VariableAttributeID = 1
        and c.StudyID = 21"

 x = sqlQuerySensory

 codemean <- function(x) {
   '%>%' = magrittr::'%>%'
   dplyr::group_by(x, .data$code)
   dplyr::summarize_at(dplyr::vars(dplyr::matches("c\\d+")), mean)
   return ()
 }
 result <- codemean(x = x)

 OutputDataSet <- result$x
如何修改dplyr脚本以生成给定响应集的c3列平均值

更新: 我已经使用收到的评论和文档中的反馈修改了大部分脚本。我还使用了一个静态StudyID进行测试,现在我收到了查询中每一列的完整摘要。修订后的TSQL语法如下所示:

ALTER PROCEDURE [dbo].[spCodeMeans]
-- Add the parameters for the stored procedure here
@StudyID int,
@StudyID_outer int OUT


AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
Declare @inquery nvarchar(max) = N'Select
        c.StudyID, c.RespID, c.ProductNumber, c.ProductSequence, c.BottomScaleValue, 
        c.BottomScaleAnchor, c.TopScaleValue, c.TopScaleAnchor, c.StudyDate,
        c.DayOfWeek, c.A, c.B, c.C, c.D, c.E, c.F,
        c.DependentVarYN, c.VariableAttributeID, c.VarAttributeName, c.[1] as c1, 
        c.[2] as c2, c.[3] as c3, c.[4] as c4, c.[5] as c5, c.[6] as c6, c.[7] as c7, c.[8] as c8
        from ClosedStudyResponses c
        --Sensory Value Attributes only for mean and standard deviation analytics.
        where VariableAttributeID = 1
        and c.StudyID = 21'
        ;
BEGIN TRY

        exec sp_execute_external_script
        @language = N'R',
        @script = N'
            OutputDataSet = data.frame(summary(InputDataSet))',
@input_data_1 = @inquery

END TRY

BEGIN CATCH
    THROW;
END CATCH
END

根据上面的存储过程,如何返回按StudyID分组的c1到c8列的平均值?

我很难理解您编写的代码。我们可以从等式中删除TSQL存储过程吗?您是否在R中编写了一些dplyr代码来查询数据库并对其执行一些简单的操作?如果你不介意我问,在这之前你写了多少R/dplyr代码?我只是想弄清楚你是从哪里来的。我认为问题不在你的dplyr脚本中,而是在你分配给
sqlQuerySensory
的任务中。整个块只是一个字符串,而不是为一个数据帧指定属性(我猜这就是你想要的)。我在您的代码中没有看到任何类似于数据表的内容,因此应用
groupby
时会出现问题。除非我在你的问题中遗漏了什么?澄清一下,我不太愿意在这里尝试任何帮助,因为对我来说,几乎没有一个代码对我来说是完全有效的,这让我想知道我是否遗漏了一些关于R代码在SQL Server内部是如何执行的…?嗯,再次假设我没有遗漏R代码在SQL Server内部运行时是如何执行的,以下是我对该代码不了解的地方:(1)没有任何代码实际将查询发送到db。通常,这将涉及DBI包。(2)
codemean()
以语句
return()
结尾,这通常在R中意味着它总是不返回任何内容。(3)
codemean()
中的3行没有为变量赋值;该代码的结果将丢失(4)我找不到
。data$code
将在哪里出现。我可以继续…在开始编写函数之前,我建议简化一些事情,以便进行一些调试。看看是否可以执行查询并返回数据帧。关于
groupby
的错误在于,函数的输入是字符串(查询),而不是执行查询返回的数据帧。在将其抽象为函数之前,先进行大量工作