rowwise correlation for specific columnsHow to sort a dataframe by multiple column(s)Grouping functions (tapply, by, aggregate) and the *apply familyDisplay Correlation Tables as Descending ListCalculate correlation by aggregating columns of data frameestimate in lm function in R doesn't match correlation (data with NA)Correlation between every column in RR: Create dataset with specific correlation in rSpearman - pairwise correlationHow to use `cor.test` for correlation of specific columns?Correlate subset of matrix for each row

King or Queen-Which piece is which?

Encounter design and XP thresholds

How to maintain a closed environment for one person for a long period of time

What is the origin of Scooby-Doo's name?

What's currently blocking the construction of the wall between Mexico and the US?

What is the oldest commercial MS-DOS program that can run on modern versions of Windows without third-party software?

Do I need a shock-proof watch for cycling?

Is there a term for the belief that "if it's legal, it's moral"?

Why is oilcloth made with linseed oil?

How can I bring back a dead main character without cliches?

How to parse 「場合でも」

How would modern naval warfare have to have developed differently for battleships to still be relevant in the 21st century?

Creating a histogram using custom data

How do I farm creepers for XP without them exploding?

Is there a difference between an NFC and RFID chip?

Why is "Congress shall have power to enforce this article by appropriate legislation" necessary?

How to execute a command when ALL of the players are close enough

Greeting with "Ho"

Helping ease my back pain by studying 13 hours everyday , even weekends

Explain why a line can never intersect a plane in exactly two points.

Music theory behind A chord in the key of G

How did Gollum enter Moria?

Can humans ever directly see a few photons at a time? Can a human see a single photon?

Ideal of strictly singular operators



rowwise correlation for specific columns


How to sort a dataframe by multiple column(s)Grouping functions (tapply, by, aggregate) and the *apply familyDisplay Correlation Tables as Descending ListCalculate correlation by aggregating columns of data frameestimate in lm function in R doesn't match correlation (data with NA)Correlation between every column in RR: Create dataset with specific correlation in rSpearman - pairwise correlationHow to use `cor.test` for correlation of specific columns?Correlate subset of matrix for each row






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















For all rows (Symbols) i would like to correlated the five columns F1_G-F5_G with the five columns F1_P-F5_P. This should give one correlation value for each Symbol.



 Symbol F1_G F2_G F3_G F4_G F5_G F1_P F2_P F3_P F4_P F5_P
1 abca2 0.7696639 1.301428 0.8447565 0.6936672 0.6987410 9.873610 9.705205 8.044027 8.311364 9.961380
2 aco2 7.4274715 7.234892 7.8543164 8.0142983 8.1512194 9.620114 9.767721 7.607115 7.854960 9.472660
3 adat3 -2.0560126 -1.536868 -0.4181457 -1.1946602 -0.7707472 8.975871 8.645235 7.926262 7.432755 8.633583
4 adat3 -2.0560126 -1.536868 -0.4181457 -1.1946602 -0.7707472 9.620114 9.237699 7.162386 7.972086 8.872763
5 adnp 1.4228436 0.932214 0.8964153 0.8125162 0.9921002 9.177645 9.323443 8.507508 8.080413 8.633583
6 arhgap8 -2.6517712 -2.067164 -1.4918958 -2.6517712 -1.5474257 9.395681 8.861322 8.333381 8.038053 8.872763


I tried something like this, but is does not consider each row:



res <- outer(df[, c(2,3,4,5,6)], df[, c(7,8,9,10,11)], function(X, Y)
mapply(function(...) cor.test(..., na.action = "na.exclude")$estimate,
X, Y)
)


out:



 Symbol Cor
abca2 0.14
aco2 0.12









share|improve this question






























    0















    For all rows (Symbols) i would like to correlated the five columns F1_G-F5_G with the five columns F1_P-F5_P. This should give one correlation value for each Symbol.



     Symbol F1_G F2_G F3_G F4_G F5_G F1_P F2_P F3_P F4_P F5_P
    1 abca2 0.7696639 1.301428 0.8447565 0.6936672 0.6987410 9.873610 9.705205 8.044027 8.311364 9.961380
    2 aco2 7.4274715 7.234892 7.8543164 8.0142983 8.1512194 9.620114 9.767721 7.607115 7.854960 9.472660
    3 adat3 -2.0560126 -1.536868 -0.4181457 -1.1946602 -0.7707472 8.975871 8.645235 7.926262 7.432755 8.633583
    4 adat3 -2.0560126 -1.536868 -0.4181457 -1.1946602 -0.7707472 9.620114 9.237699 7.162386 7.972086 8.872763
    5 adnp 1.4228436 0.932214 0.8964153 0.8125162 0.9921002 9.177645 9.323443 8.507508 8.080413 8.633583
    6 arhgap8 -2.6517712 -2.067164 -1.4918958 -2.6517712 -1.5474257 9.395681 8.861322 8.333381 8.038053 8.872763


    I tried something like this, but is does not consider each row:



    res <- outer(df[, c(2,3,4,5,6)], df[, c(7,8,9,10,11)], function(X, Y)
    mapply(function(...) cor.test(..., na.action = "na.exclude")$estimate,
    X, Y)
    )


    out:



     Symbol Cor
    abca2 0.14
    aco2 0.12









    share|improve this question


























      0












      0








      0








      For all rows (Symbols) i would like to correlated the five columns F1_G-F5_G with the five columns F1_P-F5_P. This should give one correlation value for each Symbol.



       Symbol F1_G F2_G F3_G F4_G F5_G F1_P F2_P F3_P F4_P F5_P
      1 abca2 0.7696639 1.301428 0.8447565 0.6936672 0.6987410 9.873610 9.705205 8.044027 8.311364 9.961380
      2 aco2 7.4274715 7.234892 7.8543164 8.0142983 8.1512194 9.620114 9.767721 7.607115 7.854960 9.472660
      3 adat3 -2.0560126 -1.536868 -0.4181457 -1.1946602 -0.7707472 8.975871 8.645235 7.926262 7.432755 8.633583
      4 adat3 -2.0560126 -1.536868 -0.4181457 -1.1946602 -0.7707472 9.620114 9.237699 7.162386 7.972086 8.872763
      5 adnp 1.4228436 0.932214 0.8964153 0.8125162 0.9921002 9.177645 9.323443 8.507508 8.080413 8.633583
      6 arhgap8 -2.6517712 -2.067164 -1.4918958 -2.6517712 -1.5474257 9.395681 8.861322 8.333381 8.038053 8.872763


      I tried something like this, but is does not consider each row:



      res <- outer(df[, c(2,3,4,5,6)], df[, c(7,8,9,10,11)], function(X, Y)
      mapply(function(...) cor.test(..., na.action = "na.exclude")$estimate,
      X, Y)
      )


      out:



       Symbol Cor
      abca2 0.14
      aco2 0.12









      share|improve this question
















      For all rows (Symbols) i would like to correlated the five columns F1_G-F5_G with the five columns F1_P-F5_P. This should give one correlation value for each Symbol.



       Symbol F1_G F2_G F3_G F4_G F5_G F1_P F2_P F3_P F4_P F5_P
      1 abca2 0.7696639 1.301428 0.8447565 0.6936672 0.6987410 9.873610 9.705205 8.044027 8.311364 9.961380
      2 aco2 7.4274715 7.234892 7.8543164 8.0142983 8.1512194 9.620114 9.767721 7.607115 7.854960 9.472660
      3 adat3 -2.0560126 -1.536868 -0.4181457 -1.1946602 -0.7707472 8.975871 8.645235 7.926262 7.432755 8.633583
      4 adat3 -2.0560126 -1.536868 -0.4181457 -1.1946602 -0.7707472 9.620114 9.237699 7.162386 7.972086 8.872763
      5 adnp 1.4228436 0.932214 0.8964153 0.8125162 0.9921002 9.177645 9.323443 8.507508 8.080413 8.633583
      6 arhgap8 -2.6517712 -2.067164 -1.4918958 -2.6517712 -1.5474257 9.395681 8.861322 8.333381 8.038053 8.872763


      I tried something like this, but is does not consider each row:



      res <- outer(df[, c(2,3,4,5,6)], df[, c(7,8,9,10,11)], function(X, Y)
      mapply(function(...) cor.test(..., na.action = "na.exclude")$estimate,
      X, Y)
      )


      out:



       Symbol Cor
      abca2 0.14
      aco2 0.12






      r correlation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 7:54







      user2300940

















      asked Mar 25 at 7:38









      user2300940user2300940

      749616




      749616






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Since you want to do it row-wise we can use apply with MARGIN = 1



          #Get column indices ending with G
          g_cols <- grep("G$", names(df))
          #Get column indices ending with P
          p_cols <- grep("P$", names(df))

          apply(df, 1, function(x) cor.test(as.numeric(x[g_cols]),
          as.numeric(x[p_cols]), na.action = "na.exclude")$estimate)

          # 1 2 3 4 5 6
          # 0.21890 -0.52925 -0.52776 -0.82073 0.60473 -0.11785



          A tidyverse approach would be



          library(tidyverse)

          df %>%
          mutate(row = row_number()) %>%
          select(-Symbol) %>%
          gather(key, value, -row) %>%
          group_by(row) %>%
          summarise(ans = cor.test(value[key %in% g_cols], value[key %in% p_cols],
          na.action = "na.exclude")$estimate)


          # row ans
          # <int> <dbl>
          #1 1 0.219
          #2 2 -0.529
          #3 3 -0.528
          #4 4 -0.821
          #5 5 0.605
          #6 6 -0.118





          share|improve this answer

























          • Error in cor.test.default(as.numeric(x[g_cols]), as.numeric(x[p_cols]), : not enough finite observations

            – user2300940
            Mar 25 at 7:54











          • @user2300940 So Symbol, adat3 has two rows so that would consolidate into one output only?

            – Ronak Shah
            Mar 25 at 7:56












          • Problem was NAs. Works now, but how do you intepret the colnames in your output? 1,2,3 and so on?

            – user2300940
            Mar 25 at 8:07











          • The 6 values in the output corresponds to 6 rows. For every row, we take cor.test between variables in g_cols and p_cols.

            – Ronak Shah
            Mar 25 at 8:09











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55333131%2frowwise-correlation-for-specific-columns%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Since you want to do it row-wise we can use apply with MARGIN = 1



          #Get column indices ending with G
          g_cols <- grep("G$", names(df))
          #Get column indices ending with P
          p_cols <- grep("P$", names(df))

          apply(df, 1, function(x) cor.test(as.numeric(x[g_cols]),
          as.numeric(x[p_cols]), na.action = "na.exclude")$estimate)

          # 1 2 3 4 5 6
          # 0.21890 -0.52925 -0.52776 -0.82073 0.60473 -0.11785



          A tidyverse approach would be



          library(tidyverse)

          df %>%
          mutate(row = row_number()) %>%
          select(-Symbol) %>%
          gather(key, value, -row) %>%
          group_by(row) %>%
          summarise(ans = cor.test(value[key %in% g_cols], value[key %in% p_cols],
          na.action = "na.exclude")$estimate)


          # row ans
          # <int> <dbl>
          #1 1 0.219
          #2 2 -0.529
          #3 3 -0.528
          #4 4 -0.821
          #5 5 0.605
          #6 6 -0.118





          share|improve this answer

























          • Error in cor.test.default(as.numeric(x[g_cols]), as.numeric(x[p_cols]), : not enough finite observations

            – user2300940
            Mar 25 at 7:54











          • @user2300940 So Symbol, adat3 has two rows so that would consolidate into one output only?

            – Ronak Shah
            Mar 25 at 7:56












          • Problem was NAs. Works now, but how do you intepret the colnames in your output? 1,2,3 and so on?

            – user2300940
            Mar 25 at 8:07











          • The 6 values in the output corresponds to 6 rows. For every row, we take cor.test between variables in g_cols and p_cols.

            – Ronak Shah
            Mar 25 at 8:09















          1














          Since you want to do it row-wise we can use apply with MARGIN = 1



          #Get column indices ending with G
          g_cols <- grep("G$", names(df))
          #Get column indices ending with P
          p_cols <- grep("P$", names(df))

          apply(df, 1, function(x) cor.test(as.numeric(x[g_cols]),
          as.numeric(x[p_cols]), na.action = "na.exclude")$estimate)

          # 1 2 3 4 5 6
          # 0.21890 -0.52925 -0.52776 -0.82073 0.60473 -0.11785



          A tidyverse approach would be



          library(tidyverse)

          df %>%
          mutate(row = row_number()) %>%
          select(-Symbol) %>%
          gather(key, value, -row) %>%
          group_by(row) %>%
          summarise(ans = cor.test(value[key %in% g_cols], value[key %in% p_cols],
          na.action = "na.exclude")$estimate)


          # row ans
          # <int> <dbl>
          #1 1 0.219
          #2 2 -0.529
          #3 3 -0.528
          #4 4 -0.821
          #5 5 0.605
          #6 6 -0.118





          share|improve this answer

























          • Error in cor.test.default(as.numeric(x[g_cols]), as.numeric(x[p_cols]), : not enough finite observations

            – user2300940
            Mar 25 at 7:54











          • @user2300940 So Symbol, adat3 has two rows so that would consolidate into one output only?

            – Ronak Shah
            Mar 25 at 7:56












          • Problem was NAs. Works now, but how do you intepret the colnames in your output? 1,2,3 and so on?

            – user2300940
            Mar 25 at 8:07











          • The 6 values in the output corresponds to 6 rows. For every row, we take cor.test between variables in g_cols and p_cols.

            – Ronak Shah
            Mar 25 at 8:09













          1












          1








          1







          Since you want to do it row-wise we can use apply with MARGIN = 1



          #Get column indices ending with G
          g_cols <- grep("G$", names(df))
          #Get column indices ending with P
          p_cols <- grep("P$", names(df))

          apply(df, 1, function(x) cor.test(as.numeric(x[g_cols]),
          as.numeric(x[p_cols]), na.action = "na.exclude")$estimate)

          # 1 2 3 4 5 6
          # 0.21890 -0.52925 -0.52776 -0.82073 0.60473 -0.11785



          A tidyverse approach would be



          library(tidyverse)

          df %>%
          mutate(row = row_number()) %>%
          select(-Symbol) %>%
          gather(key, value, -row) %>%
          group_by(row) %>%
          summarise(ans = cor.test(value[key %in% g_cols], value[key %in% p_cols],
          na.action = "na.exclude")$estimate)


          # row ans
          # <int> <dbl>
          #1 1 0.219
          #2 2 -0.529
          #3 3 -0.528
          #4 4 -0.821
          #5 5 0.605
          #6 6 -0.118





          share|improve this answer















          Since you want to do it row-wise we can use apply with MARGIN = 1



          #Get column indices ending with G
          g_cols <- grep("G$", names(df))
          #Get column indices ending with P
          p_cols <- grep("P$", names(df))

          apply(df, 1, function(x) cor.test(as.numeric(x[g_cols]),
          as.numeric(x[p_cols]), na.action = "na.exclude")$estimate)

          # 1 2 3 4 5 6
          # 0.21890 -0.52925 -0.52776 -0.82073 0.60473 -0.11785



          A tidyverse approach would be



          library(tidyverse)

          df %>%
          mutate(row = row_number()) %>%
          select(-Symbol) %>%
          gather(key, value, -row) %>%
          group_by(row) %>%
          summarise(ans = cor.test(value[key %in% g_cols], value[key %in% p_cols],
          na.action = "na.exclude")$estimate)


          # row ans
          # <int> <dbl>
          #1 1 0.219
          #2 2 -0.529
          #3 3 -0.528
          #4 4 -0.821
          #5 5 0.605
          #6 6 -0.118






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 25 at 8:23

























          answered Mar 25 at 7:43









          Ronak ShahRonak Shah

          61.5k104679




          61.5k104679












          • Error in cor.test.default(as.numeric(x[g_cols]), as.numeric(x[p_cols]), : not enough finite observations

            – user2300940
            Mar 25 at 7:54











          • @user2300940 So Symbol, adat3 has two rows so that would consolidate into one output only?

            – Ronak Shah
            Mar 25 at 7:56












          • Problem was NAs. Works now, but how do you intepret the colnames in your output? 1,2,3 and so on?

            – user2300940
            Mar 25 at 8:07











          • The 6 values in the output corresponds to 6 rows. For every row, we take cor.test between variables in g_cols and p_cols.

            – Ronak Shah
            Mar 25 at 8:09

















          • Error in cor.test.default(as.numeric(x[g_cols]), as.numeric(x[p_cols]), : not enough finite observations

            – user2300940
            Mar 25 at 7:54











          • @user2300940 So Symbol, adat3 has two rows so that would consolidate into one output only?

            – Ronak Shah
            Mar 25 at 7:56












          • Problem was NAs. Works now, but how do you intepret the colnames in your output? 1,2,3 and so on?

            – user2300940
            Mar 25 at 8:07











          • The 6 values in the output corresponds to 6 rows. For every row, we take cor.test between variables in g_cols and p_cols.

            – Ronak Shah
            Mar 25 at 8:09
















          Error in cor.test.default(as.numeric(x[g_cols]), as.numeric(x[p_cols]), : not enough finite observations

          – user2300940
          Mar 25 at 7:54





          Error in cor.test.default(as.numeric(x[g_cols]), as.numeric(x[p_cols]), : not enough finite observations

          – user2300940
          Mar 25 at 7:54













          @user2300940 So Symbol, adat3 has two rows so that would consolidate into one output only?

          – Ronak Shah
          Mar 25 at 7:56






          @user2300940 So Symbol, adat3 has two rows so that would consolidate into one output only?

          – Ronak Shah
          Mar 25 at 7:56














          Problem was NAs. Works now, but how do you intepret the colnames in your output? 1,2,3 and so on?

          – user2300940
          Mar 25 at 8:07





          Problem was NAs. Works now, but how do you intepret the colnames in your output? 1,2,3 and so on?

          – user2300940
          Mar 25 at 8:07













          The 6 values in the output corresponds to 6 rows. For every row, we take cor.test between variables in g_cols and p_cols.

          – Ronak Shah
          Mar 25 at 8:09





          The 6 values in the output corresponds to 6 rows. For every row, we take cor.test between variables in g_cols and p_cols.

          – Ronak Shah
          Mar 25 at 8:09



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55333131%2frowwise-correlation-for-specific-columns%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해