Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9029299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:13:04+00:00 2026-06-16T07:13:04+00:00

Possible Duplicate: In R, correlation test between two columns, for each of the groups

  • 0

Possible Duplicate:
In R, correlation test between two columns, for each of the groups in a third column

My data frame (g) contains 2 columns with continuous data and other columns with categorical data. I want to test for correlations between the 2 continuous variables, in different groups which are defined by a third column.
g (157X3000) look like:

     Geno          GDW         GN        M1     M2      M3
1 SB002XSB012 -17.1597630   52.31961    G/G    C/C     T/T
3 SB002XSB044  -3.6537657   53.81305    G/G    C/G     G/G
4 SB002XSB051  -7.8411596   58.05924    A/G    C/C     G/T
5 SB002XSB067   2.8412103   30.85074    A/G    G/G     G/T
6 SB002XSB073 -16.0789550  -10.09913    A/A    C/G     G/G
7 SB002XSB095   0.1759709   10.28837    A/A    G/G     T/T

I’m looking for the correlations between GDW and GN in each of the groups as defined by each M. I tried :

q<- function (x) {  
    spl<-split(g[,2:3],x)  
    r<-function(x) { 
        if ((nrow(x[[1]][1]))>2)  
          cor.test(x[[1]][1],x[[1]][2],use="pairwise.complete.obs")[3:4] else Na  
    }  
    cor<- sapply(spl,r)  
} 

all.cor<- apply(g[,4:ncol(g)],2,q)

and got:

Error in if ((nrow(x[[1]][1])) > 2) cor.test(x[[1]][1], x[[1]][2], use = "pairwise.complete.obs")[3:4] else Na : 
  argument is of length zero
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:13:06+00:00Added an answer on June 16, 2026 at 7:13 am

    or if you use the plyr package you could say (copying Antoine’s sample data)

    library(plyr)
    g <- data.frame(col1=rnorm(100, 1, 1), col2=rnorm(100, 10, 3), col3=c(rep("a", 50), rep("b", 50)))
    
    co <- ddply(g, .(col3), function(adf) cor(adf[,1], adf[,2]))
    

    to give a data.frame looking something like…

        col3         V1
      1    a -0.1697979
      2    b  0.1660783
    

    EDIT: adding looping for multiple columns
    *EDIT2: removing ridiculously complicated expression!*

    library(plyr)
    g <- data.frame(col1=rnorm(100, 1, 1),
                col2=rnorm(100, 10, 3),
                col3=sample(c('a','b','c'), 100, replace=TRUE),
                col4=sample(c('a','b','c'), 100, replace=TRUE),
                col5=sample(c('a','b','c'), 100, replace=TRUE),
                col6=sample(c('a','b','c'), 100, replace=TRUE))
    for (i in 3:6) {
      co <- ddply(g, i, function(adf) cor(adf[,1], adf[,2]))
      names(co) <- c('variable',paste('CorCol',i, sep='-'))
      if(exists('odf')) { 
        odf <- merge(odf, co, by='variable', all=TRUE)
      } else {
        odf <- co
      } 
    } 
    

    Results are in data.frame odf with a column for each correlation so looks like:

    > odf
      variable    CorCol-3    CorCol-4    CorCol-5    CorCol-6
    1        a  0.29596471 -0.12278082  0.02184259  0.11972933
    2        b -0.11793616  0.08827011  0.11030097 -0.03682823
    3        c -0.09552299  0.12951251 -0.03855727 -0.03082486
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Trying to get tables next to each other horizontal I have two
Possible Duplicate: Difference between declaration and malloc Is there a difference between these two
Possible Duplicate: correlation matrix in r I have an excel sheet which has 700
Possible Duplicate: Does every Core Data Relationship have to have an Inverse? I have
Possible Duplicate: I have a nested data structure / JSON, how can I access
Possible Duplicate: Matlab Cross correlation vs Correlation Coefficient question When I cross correlate 2
Possible Duplicate: nth-child for every two table rows I'm trying to work my way
Possible Duplicate: Matching an integer between the brackets What is the regex for matching
Possible Duplicate: ggplot2: Adding Regression Line Equation and R2 on graph I'm graphing data
Possible Duplicate: Best way to determine if two path reference to same file in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.