I have some legacy code for calculating correlations and now want to use a R solution as it is faster.
Having the following code:
DROP TABLE IF EXISTS #DummyData
CREATE TABLE #DummyData
(
     [VariableA] VARCHAR(24)
    ,[VariableB] VARCHAR(24)
    ,[Value] SMALLINT
)
INSERT INTO #DummyData([VariableA], [VariableB], [Value])
VALUES   ('A1','B1', 4)
        ,('A1','B2', 3)
        ,('A1','B3', 1)
        ,('A2','B1', 2)
        ,('A2','B2', 1)
        ,('A2','B3', 3)
        ,('A3','B1', 4)
        ,('A3','B2', 5)
        ,('A3','B3', 2);
EXECUTE sp_execute_external_script    
      @language = N'R'   
    , @script = N'
         library(reshape)
         pivotData <- cast(DataIn, VariableA ~ VariableB,fun.aggregate = max)
         curData <- cor(pivotData)
         DataOut <- data.frame(curData)
    '   
    , @input_data_1 = N'SELECT [VariableA], [VariableB], [Value] FROM #DummyData'
    , @input_data_1_name  = N'DataIn'   
    , @output_data_1_name =  N'DataOut';
we have this output:
Is there a way to turn in into this one using some R library function?


 
     
    