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

后端 未结 2 1076
渐次进展
渐次进展 2021-01-07 15:10

My data frame (g) contains 2 columns with continues data and other columns with categorical data. I want to test for correlations between the 2 continues variables, in diffe

相关标签:
2条回答
  • 2021-01-07 15:35

    I think you want this inside of function r:

    if ((length(x[[1]]))>2)
        cor.test(x[[1]],x[[2]],use="pairwise.complete.obs")[3:4] else NA
    

    x[[1]] is a vector, whereas x[[1]][1] is a single element of that vector. You clearly want vectors for cor.test and not single elements.

    In addition, a vector has a length but nrow is not appropriate.

    The error that you get is a result of nrow(x[[1]][1]) evaluating to NULL, so nrow(x[[1]][1]) > 2 evaluates to logical(0). The argument to if should be a logical of length 1.

    0 讨论(0)
  • 2021-01-07 15:43

    If I understand right, you want to compute the correlations between GDW and GN for every value in the column M1 (that is, by splitting at every unique value of M1).

    Using Hadley's plyr

    require(plyr)
    # assuming the data.frame is df (Note: factor M1 if necessary)
    daply(df, .(M1), function(y) cor(y$GDW, y$GN))
    
    0 讨论(0)
提交回复
热议问题