Split a numeric dataframe into all possible combinations of 2 columns in R

泪湿孤枕 提交于 2019-12-11 11:33:08

问题


I am trying to split the columns of a dataframe to find pmcc of all possible combinations of (two)columns from a dataframe containing n columns, e.g. in this case, with 3 columns Length Diameter Height

0.455 0.365 0.095
0.350 0.265 0.090
0.530 0.420 0.135
0.440 0.365 0.125
0.330 0.255 0.22

here I have to find pmcc for all combinations, eg, (length, diameter), (diameter, height), etc. Any help! Thanks


回答1:


data.frame(z = rnorm(100, 2), y = rnorm(100, 4), x = rnorm(100, 6)) -> frame
combn(colnames(frame), 2) -> combos
apply(combos, 2, function(x) cor(frame[,x[1]], frame[,x[2]]))


来源:https://stackoverflow.com/questions/33313486/split-a-numeric-dataframe-into-all-possible-combinations-of-2-columns-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!