如何重塑R(非透视结果数据)中的相关输出?

时间:2018-07-24 13:55:00

标签: r sql-server tsql sql-server-2016 pearson-correlation

我有一些用于计算相关性的旧代码,现在想使用R解决方案,因为它更快。

具有以下代码:

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';

我们有以下输出:

enter image description here

有没有一种使用某些R库函数的方法?

enter image description here

2 个答案:

答案 0 :(得分:2)

如果将结果作为矩阵,则可以执行以下操作:

## Set the row and column names
dimnames(mat) <- list(rownames(mat, do.NULL = FALSE, prefix = "B"),
                      colnames(mat, do.NULL = FALSE, prefix = "B"))

## melt and set the variable names as desired
setNames(reshape2::melt(mat), c("VariableB_1", "VariableB_2", "Rho"))

#   VariableB_1 VariableB_2   Rho
# 1          B1          B1  1.00
# 2          B2          B1  0.86
# 3          B3          B1 -0.86
# 4          B1          B2  0.86
# 5          B2          B2  1.00
# 6          B3          B2 -0.50
# 7          B1          B3 -0.86
# 8          B2          B3 -0.50
# 9          B3          B3  1.00

答案 1 :(得分:0)

您可以运行:

int main()
{
    // Load the image
    cv::Mat img = cv::imread("path/to/the/image.png", 0);
    img.convertTo(img, CV_32FC1);

    // Convert cv::Mat to arma::fmat
    arma::fmat arma_img(reinterpret_cast<float*>(img.data), img.cols, img.rows);

    // Check if the image back from armadillo is okay
    cv::Mat opencv_img(arma_img.n_cols, arma_img.n_rows, CV_32FC1, arma_img.memptr());

    // ------ Perform SVD with OpenCV (2.5s)
    cv::SVD svvd;
    cv::Mat w1, U1, V1t;
    svvd.compute(opencv_img, w1, U1, V1t);

    cv::Mat W1 = cv::Mat::zeros(w1.rows, w1.rows, CV_32FC1);
    for (int i = 0; i<w1.rows; i++)
    {
        W1.at<float>(i, i) = w1.at<float>(i);
    }
    cv::Mat opencv_img_result = U1 * W1 * V1t;

    // ------ Perform SVD with Armadillo (0.05s)
    arma::fmat U2, V2;
    arma::fvec w2;
    arma::svd(U2, w2, V2, arma_img);

    arma::fmat W2 = arma::zeros<arma::fmat>(arma_img.n_rows, arma_img.n_cols);
    for (int i = 0; i < arma_img.n_cols; i++)
    {
        *(W2.memptr() + i * (1 + arma_img.n_rows)) = *(w2.memptr() + i);
    }
    arma::fmat arma_img_result = U2 * W2* V2.t();

    return 0;
}

结果:

library(tidyverse)

DataOut %>% 
rownames_to_column("variable_b1") %>% 
gather(key = variable_b2, val = Rho, -variable_b1)