相关矩阵错误:为什么删除 NA 仍然不起作用?

问题描述 投票:0回答:0

我正在尝试制作相关矩阵

这里是数据集的样本。

> head(matrix)
# A tibble: 6 x 16
# Groups:   nquest, nord [6]
  nquest  nord   sex anasc  ireg   eta staciv studio asnonoc2 nace2 nesplav etalav dislav acontrib occnow tpens
   <int> <int> <dbl> <int> <int> <int>  <int> <fct>     <int> <int> <fct>   <fct>  <fct>     <int>  <int> <int>
1    173     1     1  1948    18    72      3 2             2    19 1       2      0            35      2  1800
2   2886     1     1  1949    13    71      1 2             2    16 1       2      0            35      2  1211
3   2886     2     0  1952    13    68      1 3             2    17 1       2      0            42      2  2100
4   5416     1     0  1958     8    62      3 1             1    19 2       1      0            30      2   700
5   7886     1     1  1950     9    70      1 2             2    11 1       2      0            35      2  2000
6  20297     1     1  1960     5    60      1 1             1    19 2       1      0            39      2  1200

实际上,

nquest
nord
是识别码:第一个是家庭,第二个是那个特定家庭的成员。即使我尝试删除它们(因为我认为它们在相关矩阵中没有用),dplyr 会自动添加它们

matrix <- final %>%
           select("sex", "anasc", "ireg", "eta","staciv", "studio", "asnonoc2", 
                  "nace2", "nesplav", "etalav", "dislav", "acontrib", "occnow",
                  "tpens")

Dplyr 答案

Adding missing grouping variables: `nquest`, `nord`

但是,如果它们保留在数据集中,我认为这不是问题。

我的目标是计算相关矩阵,但这个数据集似乎有一些 NA 值

> sum(is.na(matrix))
[1] 109

我试过这些代码,但没有一个有效。

第一个

cor(matrix, use = "pairwise.complete.obs")

R 回复

Error in cor(matrix, use = "pairwise.complete.obs") : 
  'x' must be numeric

第二个

cor(na.omit(matrix))

R 答案

Error in cor(na.omit(matrix)) : 'x' must be numeric

我也试过了

matrix <- as.numeric(matrix)

但我得到另一种错误

Error: 'list' object cannot be coerced to type 'double'

我该如何解决? 我做错了什么?

r dplyr correlation na numeric
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.