r 相关矩阵长格式
r correlation matrix long format
这可能是关于在 R 中估计相关矩阵的第百万个问题,很抱歉,我无法找到问题的答案。
我正在尝试估计不同猪(4601、4602 等)体重之间的相关性
这是我的数据集
library("geepack")
data(dietox)
df <- dietox[, c("Weight", "Pig")]
head(df)
Weight Pig
67.00000 4601
76.59998 4601
86.50000 4601
91.59998 4601
98.59998 4601
30.09999 4602
38.29999 4602
44.50000 4602
. .
. .
. .
这是我的代码
df %>%
group_by(Pig) %>%
cor()
我收到一个错误,Error in cor(.) : 'x' must be numeric
,非常感谢您的帮助。谢谢
也许,我们需要重新整形为 'wide' 格式
library(dplyr)
library(tidyr)
library(data.table)
df %>%
mutate(rn = rowid(Pig)) %>%
pivot_wider(names_from = rn, values_from = Weight) %>%
column_to_rownames("Pig") %>%
as.matrix %>%
cor
-输出
1 2 3 4 5 6 7 8 9 10 11 12
1 1.0000000 0.9222855 0.9089571 0.8672937 0.8135320 0.7363923 0.7408283 0.7516862 0.7175035 0.6834182 0.7003925 NA
2 0.9222855 1.0000000 0.9558717 0.9157019 0.8600880 0.7859677 0.7955397 0.7830242 0.7404776 0.6852512 0.6953134 NA
3 0.9089571 0.9558717 1.0000000 0.9515235 0.8965352 0.8293146 0.8136445 0.7945452 0.7745692 0.7195725 0.7247124 NA
4 0.8672937 0.9157019 0.9515235 1.0000000 0.9577877 0.9188810 0.8950779 0.8803371 0.8581694 0.8064507 0.8104515 NA
5 0.8135320 0.8600880 0.8965352 0.9577877 1.0000000 0.9665819 0.9369499 0.9139555 0.8983066 0.8185975 0.8337903 NA
6 0.7363923 0.7859677 0.8293146 0.9188810 0.9665819 1.0000000 0.9568397 0.9327316 0.9280462 0.8538419 0.8557321 NA
7 0.7408283 0.7955397 0.8136445 0.8950779 0.9369499 0.9568397 1.0000000 0.9688745 0.9556239 0.8860879 0.8914012 NA
8 0.7516862 0.7830242 0.7945452 0.8803371 0.9139555 0.9327316 0.9688745 1.0000000 0.9657392 0.8894930 0.8929204 NA
9 0.7175035 0.7404776 0.7745692 0.8581694 0.8983066 0.9280462 0.9556239 0.9657392 1.0000000 0.9192849 0.9352723 NA
10 0.6834182 0.6852512 0.7195725 0.8064507 0.8185975 0.8538419 0.8860879 0.8894930 0.9192849 1.0000000 0.9358353 NA
11 0.7003925 0.6953134 0.7247124 0.8104515 0.8337903 0.8557321 0.8914012 0.8929204 0.9352723 0.9358353 1.0000000 NA
12 NA NA NA NA NA NA NA NA NA NA NA 1
或者如果它应该带有ID
out <- df %>%
mutate(rn = rowid(Pig)) %>%
pivot_wider(names_from = Pig, values_from = Weight) %>%
column_to_rownames("rn") %>%
as.matrix %>%
cor
dim(out)
[1] 72 72
这可能是关于在 R 中估计相关矩阵的第百万个问题,很抱歉,我无法找到问题的答案。
我正在尝试估计不同猪(4601、4602 等)体重之间的相关性
这是我的数据集
library("geepack")
data(dietox)
df <- dietox[, c("Weight", "Pig")]
head(df)
Weight Pig
67.00000 4601
76.59998 4601
86.50000 4601
91.59998 4601
98.59998 4601
30.09999 4602
38.29999 4602
44.50000 4602
. .
. .
. .
这是我的代码
df %>%
group_by(Pig) %>%
cor()
我收到一个错误,Error in cor(.) : 'x' must be numeric
,非常感谢您的帮助。谢谢
也许,我们需要重新整形为 'wide' 格式
library(dplyr)
library(tidyr)
library(data.table)
df %>%
mutate(rn = rowid(Pig)) %>%
pivot_wider(names_from = rn, values_from = Weight) %>%
column_to_rownames("Pig") %>%
as.matrix %>%
cor
-输出
1 2 3 4 5 6 7 8 9 10 11 12
1 1.0000000 0.9222855 0.9089571 0.8672937 0.8135320 0.7363923 0.7408283 0.7516862 0.7175035 0.6834182 0.7003925 NA
2 0.9222855 1.0000000 0.9558717 0.9157019 0.8600880 0.7859677 0.7955397 0.7830242 0.7404776 0.6852512 0.6953134 NA
3 0.9089571 0.9558717 1.0000000 0.9515235 0.8965352 0.8293146 0.8136445 0.7945452 0.7745692 0.7195725 0.7247124 NA
4 0.8672937 0.9157019 0.9515235 1.0000000 0.9577877 0.9188810 0.8950779 0.8803371 0.8581694 0.8064507 0.8104515 NA
5 0.8135320 0.8600880 0.8965352 0.9577877 1.0000000 0.9665819 0.9369499 0.9139555 0.8983066 0.8185975 0.8337903 NA
6 0.7363923 0.7859677 0.8293146 0.9188810 0.9665819 1.0000000 0.9568397 0.9327316 0.9280462 0.8538419 0.8557321 NA
7 0.7408283 0.7955397 0.8136445 0.8950779 0.9369499 0.9568397 1.0000000 0.9688745 0.9556239 0.8860879 0.8914012 NA
8 0.7516862 0.7830242 0.7945452 0.8803371 0.9139555 0.9327316 0.9688745 1.0000000 0.9657392 0.8894930 0.8929204 NA
9 0.7175035 0.7404776 0.7745692 0.8581694 0.8983066 0.9280462 0.9556239 0.9657392 1.0000000 0.9192849 0.9352723 NA
10 0.6834182 0.6852512 0.7195725 0.8064507 0.8185975 0.8538419 0.8860879 0.8894930 0.9192849 1.0000000 0.9358353 NA
11 0.7003925 0.6953134 0.7247124 0.8104515 0.8337903 0.8557321 0.8914012 0.8929204 0.9352723 0.9358353 1.0000000 NA
12 NA NA NA NA NA NA NA NA NA NA NA 1
或者如果它应该带有ID
out <- df %>%
mutate(rn = rowid(Pig)) %>%
pivot_wider(names_from = Pig, values_from = Weight) %>%
column_to_rownames("rn") %>%
as.matrix %>%
cor
dim(out)
[1] 72 72