如何使用rxSummary按类别分组?

时间:2018-06-17 13:31:10

标签: r sql-server tsql

我的数据库中有一个表格,如下所示: enter image description here

我编写了我的R脚本,使用rxSummary来计算平均值和标准差。我的目的是按产品编号分组,并计算第1列到第6列的平均值和标准偏差。

到目前为止我的R脚本如下:

ALTER PROCEDURE [dbo].[spCodeMeansRevo]
@StudyID int
AS
BEGIN

Declare @sStudy varchar(50)
Set @sStudy = Convert(varchar(50), @StudyID) 

Declare @inquery nvarchar(max) = N'
Select
        c.ProductNumber, c.[1] as c1, 
        c.[2] as c2, c.[3] as c3, c.[4] as c4, c.[5] as c5, c.[6] as c6
        from ClosedStudyResponses c
        where
        c.StudyID = ' + @sStudy ;

EXEC sp_execute_external_script @language = N'R'
, @script = N'

studies <- InputDataSet
studiesdf <- data.frame(studies)

sumOut <- rxSummary(c1 ~ ProductNumber, data = studiesdf, summaryStats = 
c("Mean","StdDev"))

res <- sumOut$sDataFrame
'


,@input_data_1 = @inquery
,@output_data_1_name = N'res'
,@params = N'@StudyID int'
,@StudyID = @StudyID

END;

如何正确计算按产品编号分组的第1列到第6列中每个值的平均值和标准差?

0 个答案:

没有答案