如何计算数据帧的一行与其余行之间的相关性

时间:2019-03-16 01:49:19

标签: r

我有这样的数据

 name  col1  col2  col3
1    a 43.78 43.80 43.14
2    b 43.84 43.40 42.85
3    c 37.92 37.64 37.54
4    d 31.72 31.62 31.74

让它称为df

df<-structure(list(name = structure(1:4, .Label = c("a", "b", "c", 
"d"), class = "factor"), col1 = c(43.78, 43.84, 37.92, 31.72), 
    col2 = c(43.8, 43.4, 37.64, 31.62), col3 = c(43.14, 42.85, 
    37.54, 31.74)), class = "data.frame", row.names = c(NA, -4L
))

现在我要计算 d 行与其他行

之间的R2和调整后的R2

如果我想查看所有组合,可以进行以下相关性关联

out <- cor(t(df[, -1]))
out[upper.tri(out, diag = TRUE)] <- NA
rownames(out) <- colnames(out) <- df$name
out <- na.omit(reshape::melt(t(out)))
out <- out[ order(out$X1, out$X2), ]

这给了我

   X1 X2      value
5   a  b  0.8841255
9   a  c  0.6842705
13  a  d -0.6491118
10  b  c  0.9457125
14  b  d -0.2184630
15  c  d  0.1105508

但是我只想要在d行和其余行之间,而且我还希望同时具有相关系数和调整后的R2

2 个答案:

答案 0 :(得分:1)

如果我理解正确,那么您希望d与其余每个列之间都具有相关性。

(M <- t(as.matrix(`rownames<-`(df1[-1], df$name))))
#          a     b     c     d
# col1 43.78 43.84 37.92 31.72
# col2 43.80 43.40 37.64 31.62
# col3 43.14 42.85 37.54 31.74

由于矢量化,我们可以很容易地计算d与其余数之间的相关性:

out <- t(cor(M[, 4], M[, -4]))

R 2 只是相关性的平方(Ref.),我们可以cbind进行相关。

`colnames<-`(cbind(out, out^2), c("cor", "r2"))
#          cor         r2
# a -0.6491118 0.42134617
# b -0.2184630 0.04772607
# c  0.1105508 0.01222148

注意:如果您想了解`colnames<-`表单,则可能需要阅读"Advanced R: 6.8.4 Replacement functions"。)


数据

df1 <- structure(list(name = structure(1:4, .Label = c("a", "b", "c", 
"d"), class = "factor"), col1 = c(43.78, 43.84, 37.92, 31.72), 
    col2 = c(43.8, 43.4, 37.64, 31.62), col3 = c(43.14, 42.85, 
    37.54, 31.74)), class = "data.frame", row.names = c(NA, -4L
))

答案 1 :(得分:0)

如果先转置数据帧,会更容易。之后,使用purrr::mapbroom::tidy完成工作

library(tidyverse)

df <- structure(list(name = structure(1:4, .Label = c("a", "b", "c", 
"d"), class = "factor"), col1 = c(43.78, 43.84, 37.92, 31.72), 
    col2 = c(43.8, 43.4, 37.64, 31.62), col3 = c(43.14, 42.85, 
    37.54, 31.74)), class = "data.frame", row.names = c(NA, -4L
))

# transpose df
df_transpose <- df %>% 
  gather(variable, value, -name) %>% 
  spread(name, value) %>% 
  select(-variable)

# loop through columns, apply `cor` vs 'd' column
colnames(df_transpose) %>%
  set_names() %>% 
  map(~ cor(df_transpose[, .x], df_transpose[, 'd'])) %>%
  map_dfr(., broom::tidy, .id = "var")

#> # A tibble: 4 x 2
#>   var        x
#>   <chr>  <dbl>
#> 1 a     -0.649
#> 2 b     -0.218
#> 3 c      0.111
#> 4 d      1

reprex package(v0.2.1.9000)于2019-03-15创建