两个 Dataframe 或矩阵之间的逐行Pearson相关性

juud5qan  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(89)

我想找出bvalkirp_expr之间的平均Pearson相关性。

meth_exp_corr <- diag(cor(bval, as.numeric(kirp_expr), method="pearson"))$estimate # Row-wise correlation

字符串
回溯:

Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'diag': error in evaluating the argument 'y' in selecting a method for function 'cor': 'list' object cannot be coerced to type 'double'


数据来源:

> dput(bval[1:5,1:5])
structure(c(0.936541813395938, 0.056245209738118, 0.0530093175522538, 
0.528332559219693, 0.200359336965341, 0.884098121190752, 0.0587209886180834, 
0.0157260359008846, 0.503125336781981, 0.147287104179572, 0.903812646370023, 
0.0485626039439297, 0.0437850437850438, 0.554738132772815, 0.134233917541305, 
0.955365812154032, 0.0407083939835032, 0.0617595436594998, 0.224848934591328, 
0.116850371010734, 0.832430340557275, 0.348511149686206, 0.650490852298081, 
0.620003210788248, 0.142329020332717), dim = c(5L, 5L), dimnames = list(
    c("cg00001349", "cg00001583", "cg00002719", "cg00002837", 
    "cg00003287"), c("TCGA.2K.A9WE.01", "TCGA.2Z.A9J1.01", "TCGA.2Z.A9J3.01", 
    "TCGA.2Z.A9J6.01", "TCGA.2Z.A9J7.01")))

> dput(kirp_expr[1:5,1:5])
structure(list(TCGA.2K.A9WE.01 = c(7.65342121905285, 14.3511850042327, 
10.3737643425674, 10.0819596419255, 9.44832324553208), TCGA.2Z.A9J1.01 = c(5.09389393824392, 
12.4136523086721, 11.1918237390263, 10.1912122382252, 9.9623840324273
), TCGA.2Z.A9J3.01 = c(4.70168212029528, 10.1689338100564, 9.96839262629172, 
9.87305770150294, 9.75535162798678), TCGA.2Z.A9J6.01 = c(5.13719199914349, 
12.0367193976262, 10.8202555636581, 10.3262700402849, 9.91216810777653
), TCGA.2Z.A9J7.01 = c(6.95117512427229, 10.9656928148969, 10.5991523452113, 
10.4556415168452, 9.46537845450025)), row.names = c("ENSG00000121410", 
"ENSG00000175899", "ENSG00000128274", "ENSG00000291836", "ENSG00000081760"
), class = "data.frame")


尺寸:

> dim(bval)
[1] 65737   172
> dim(kirp_expr)
[1] 11537   172

waxmsbnn

waxmsbnn1#

您不能在data.frame上使用as.numeric

as.numeric(iris)
#> Error: 'list' object cannot be coerced to type 'double'

字符串
相反,您需要将kirp_expr转换为matrix

diag(cor(bval, as.matrix(kirp_expr), method="pearson"))
#> TCGA.2K.A9WE.01 TCGA.2Z.A9J1.01 TCGA.2Z.A9J3.01 TCGA.2Z.A9J6.01 TCGA.2Z.A9J7.01 
#>      -0.7199122      -0.9051944      -0.8452573      -0.9731041      -0.4481620


但是,这只是因为两个示例对象中的行数相同,所以才起作用。如果在真实的数据上尝试此操作,您将得到“不兼容的维度”错误。如果你有不同数量的行,你的相关性是什么意思?

相关问题