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

后端 未结 2 1078
渐次进展
渐次进展 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.

提交回复
热议问题