如何创建具有多个特定变量的相关矩阵?

发布于 2025-01-18 19:35:10 字数 493 浏览 1 评论 0原文

我有一个很大的数据集,但是想执行一个相关矩阵,其中只有该数据集中的10个变量(并将变量名称命名为其他内容),

我的当前代码看起来像这样:

#Correlation Function
data.cor = cor(df)

#Correlation Coefficient
data.cor = cor(df, method = "pearson", use = "complete.obs")


#Running Correlation w/ P-Values
install.packages("Hmisc")
library("Hmisc")
data.rcorr = rcorr(as.matrix(df))
data.rcorr

#
data.coeff = data.rcorr$r
data.p = data.rcorr$P

但是,1) t工作2)我知道这只是参考完整数据集,

我该如何制作它,以便我只参考这10个变量并能够重命名变量?

事先谢谢你!

I have a very large data set, but want to do a correlation matrix with only 10 of the variables in that data set (and rename the variable names to something else)

My current code is looking like this:

#Correlation Function
data.cor = cor(df)

#Correlation Coefficient
data.cor = cor(df, method = "pearson", use = "complete.obs")


#Running Correlation w/ P-Values
install.packages("Hmisc")
library("Hmisc")
data.rcorr = rcorr(as.matrix(df))
data.rcorr

#
data.coeff = data.rcorr$r
data.p = data.rcorr$P

However, 1) it doesn't work 2) i am aware it is just reference the full data set

How can I make it so that I am referencing only those 10 variables and be able to rename the variables?

Thank you beforehand!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤星 2025-01-25 19:35:10

如果您有一个数据框d,您可以通过多种方式直接进行子集化。例如,使用变量名向量,如下所示:

target_vars = c("v1", "v2", "v7", "v8", "v12")
cor(d[,target_vars], method = "pearson", use="complete.obs")

输出:

             v1         v2          v7         v8         v12
v1   1.00000000 0.57761512 -0.22465288  0.6571388  0.06053092
v2   0.57761512 1.00000000  0.08178772  0.4641090  0.14373593
v7  -0.22465288 0.08178772  1.00000000  0.3035047 -0.46410860
v8   0.65713875 0.46410902  0.30350472  1.0000000 -0.48583862
v12  0.06053092 0.14373593 -0.46410860 -0.4858386  1.00000000

输入:

set.seed(123)
d = setNames(as.data.frame(lapply(1:20, \(x) rnorm(10))), paste0("v",1:20))

If you have a dataframe, d, you can directly subset in numerous ways. For example, using a vector of variable names, as below:

target_vars = c("v1", "v2", "v7", "v8", "v12")
cor(d[,target_vars], method = "pearson", use="complete.obs")

Output:

             v1         v2          v7         v8         v12
v1   1.00000000 0.57761512 -0.22465288  0.6571388  0.06053092
v2   0.57761512 1.00000000  0.08178772  0.4641090  0.14373593
v7  -0.22465288 0.08178772  1.00000000  0.3035047 -0.46410860
v8   0.65713875 0.46410902  0.30350472  1.0000000 -0.48583862
v12  0.06053092 0.14373593 -0.46410860 -0.4858386  1.00000000

Input:

set.seed(123)
d = setNames(as.data.frame(lapply(1:20, \(x) rnorm(10))), paste0("v",1:20))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文