查找列的相关性
Finding correlations of columns
前言: 我是R的初学者,渴望学习。请不要将问题的简单性(如果它是一个简单的答案)误认为是缺乏研究或努力!
我有一个名为 data
的 data.table,其列标记为 V1 到 V20。我想获取某些列(1、6、7、9、10 和 11)相对于 V18 的相关性,这样我就有了一个新的 table,如下所示
Variable Correlation_to_V18
V1 cor(V1,V18)
V6 cor(V6,V18)
V7 cor(V7,V18)
V9 cor(V9,V18)
V10 cor(V10,V18)
V11 cor(V11,V18)
我试过使用 for 循环没有用
column <- c(1,6,7,9,10,11)
for (i in column) {
correlations<-cor(data[,18], data[,as.numeric(i)])
cor_table<- data.table(variables = colnames(data[,as.numeric(column)]), correlation_val = correlations)
return(cor_table)
}
让我们制作一些听起来像您的数据的数据(50 行 x 20 列):
library(dplyr)
library(tidyr)
library(corrr)
set.seed(123)
df1 <- data.frame(matrix(rnorm(1000),
ncol = 20,
nrow = 50,
dimnames = list(c(), paste0("V", 1:20))))
一般来说,在使用 R 时,使用循环处理数据帧不是一个好主意。有 apply
个函数、tidyverse
个包和其他包可以更轻松地处理数据框。
在这种情况下,我将使用 corrr
库来 return 具有相关系数的数据帧。然后,您可以过滤感兴趣的术语 V18
,将输出从“宽”重塑为“长”,然后再次过滤其他变量。
correlate(df1) %>%
filter(term == "V18") %>%
pivot_longer(2:21) %>%
filter(name %in% c("V1", "V6", "V7", "V9", "V10", "V11"))
结果:
# A tibble: 6 x 3
term name value
<chr> <chr> <dbl>
1 V18 V1 0.125
2 V18 V6 -0.167
3 V18 V7 0.345
4 V18 V9 -0.110
5 V18 V10 0.0473
6 V18 V11 -0.0347
这是另一个可能的解决方案:
library(data.table)
set.seed(123)
mat <- matrix(runif(400), nrow = 20, ncol = 20)
data <- data.table(mat)
columns_of_interest <- c(1,6,7,9,10,11,18)
data_subset <- data[, columns_of_interest, with=FALSE]
data_cor_values <- data_subset[, .(Correlation_to_V18 = cor(.SD, V18))]
data_cor_values[, variable := columns_of_interest]
data_cor_values
#> Correlation_to_V18.V1 variable
#> 1: -0.049812188 1
#> 2: -0.375566877 6
#> 3: 0.089879501 7
#> 4: -0.022499113 9
#> 5: -0.007267059 10
#> 6: -0.178489961 11
#> 7: 1.000000000 18
# Trim the last row (V18)
data_cor_values[1:.N-1,]
#> Correlation_to_V18.V1 variable
#> 1: -0.049812188 1
#> 2: -0.375566877 6
#> 3: 0.089879501 7
#> 4: -0.022499113 9
#> 5: -0.007267059 10
#> 6: -0.178489961 11
# Check the answer is correct:
cor(data_subset$V1, data_subset$V18)
#> [1] -0.04981219
cor(data_subset$V6, data_subset$V18)
#> [1] -0.3755669
由 reprex package (v2.0.1)
于 2021-11-30 创建
这对于 apply 系列来说相当简单。感谢用户neilfws提供数据+1
set.seed(123)
df <- data.frame(matrix(rnorm(1000), ncol = 20, nrow = 50, dimnames = list(c(), paste0("V", 1:20))))
stack(lapply(df[names(df)!="V18"], function(x) cor(df$V18, x)))
#> values ind
#> 1 1.247235e-01 V1
#> 2 -3.219809e-02 V2
#> 3 -9.556828e-05 V3
#> 4 -1.912672e-01 V4
#> 5 -7.489594e-02 V5
#> 6 -1.669195e-01 V6
#> 7 3.449937e-01 V7
#> 8 -1.968900e-01 V8
#> 9 -1.099080e-01 V9
#> 10 4.732393e-02 V10
#> 11 -3.465666e-02 V11
#> 12 2.002576e-01 V12
#> 13 -4.510807e-02 V13
#> 14 -2.545680e-01 V14
#> 15 -1.679573e-03 V15
#> 16 7.318178e-02 V16
#> 17 6.446624e-02 V17
#> 18 1.999834e-01 V19
#> 19 1.120717e-01 V20
您在没有感兴趣的列的情况下循环遍历数据框。当您决定使用 lapply 时,您最终会得到一个列表,您可以使用 stack
.
轻松地将其堆叠到数据框
前言: 我是R的初学者,渴望学习。请不要将问题的简单性(如果它是一个简单的答案)误认为是缺乏研究或努力!
我有一个名为 data
的 data.table,其列标记为 V1 到 V20。我想获取某些列(1、6、7、9、10 和 11)相对于 V18 的相关性,这样我就有了一个新的 table,如下所示
Variable Correlation_to_V18
V1 cor(V1,V18)
V6 cor(V6,V18)
V7 cor(V7,V18)
V9 cor(V9,V18)
V10 cor(V10,V18)
V11 cor(V11,V18)
我试过使用 for 循环没有用
column <- c(1,6,7,9,10,11)
for (i in column) {
correlations<-cor(data[,18], data[,as.numeric(i)])
cor_table<- data.table(variables = colnames(data[,as.numeric(column)]), correlation_val = correlations)
return(cor_table)
}
让我们制作一些听起来像您的数据的数据(50 行 x 20 列):
library(dplyr)
library(tidyr)
library(corrr)
set.seed(123)
df1 <- data.frame(matrix(rnorm(1000),
ncol = 20,
nrow = 50,
dimnames = list(c(), paste0("V", 1:20))))
一般来说,在使用 R 时,使用循环处理数据帧不是一个好主意。有 apply
个函数、tidyverse
个包和其他包可以更轻松地处理数据框。
在这种情况下,我将使用 corrr
库来 return 具有相关系数的数据帧。然后,您可以过滤感兴趣的术语 V18
,将输出从“宽”重塑为“长”,然后再次过滤其他变量。
correlate(df1) %>%
filter(term == "V18") %>%
pivot_longer(2:21) %>%
filter(name %in% c("V1", "V6", "V7", "V9", "V10", "V11"))
结果:
# A tibble: 6 x 3
term name value
<chr> <chr> <dbl>
1 V18 V1 0.125
2 V18 V6 -0.167
3 V18 V7 0.345
4 V18 V9 -0.110
5 V18 V10 0.0473
6 V18 V11 -0.0347
这是另一个可能的解决方案:
library(data.table)
set.seed(123)
mat <- matrix(runif(400), nrow = 20, ncol = 20)
data <- data.table(mat)
columns_of_interest <- c(1,6,7,9,10,11,18)
data_subset <- data[, columns_of_interest, with=FALSE]
data_cor_values <- data_subset[, .(Correlation_to_V18 = cor(.SD, V18))]
data_cor_values[, variable := columns_of_interest]
data_cor_values
#> Correlation_to_V18.V1 variable
#> 1: -0.049812188 1
#> 2: -0.375566877 6
#> 3: 0.089879501 7
#> 4: -0.022499113 9
#> 5: -0.007267059 10
#> 6: -0.178489961 11
#> 7: 1.000000000 18
# Trim the last row (V18)
data_cor_values[1:.N-1,]
#> Correlation_to_V18.V1 variable
#> 1: -0.049812188 1
#> 2: -0.375566877 6
#> 3: 0.089879501 7
#> 4: -0.022499113 9
#> 5: -0.007267059 10
#> 6: -0.178489961 11
# Check the answer is correct:
cor(data_subset$V1, data_subset$V18)
#> [1] -0.04981219
cor(data_subset$V6, data_subset$V18)
#> [1] -0.3755669
由 reprex package (v2.0.1)
于 2021-11-30 创建这对于 apply 系列来说相当简单。感谢用户neilfws提供数据+1
set.seed(123)
df <- data.frame(matrix(rnorm(1000), ncol = 20, nrow = 50, dimnames = list(c(), paste0("V", 1:20))))
stack(lapply(df[names(df)!="V18"], function(x) cor(df$V18, x)))
#> values ind
#> 1 1.247235e-01 V1
#> 2 -3.219809e-02 V2
#> 3 -9.556828e-05 V3
#> 4 -1.912672e-01 V4
#> 5 -7.489594e-02 V5
#> 6 -1.669195e-01 V6
#> 7 3.449937e-01 V7
#> 8 -1.968900e-01 V8
#> 9 -1.099080e-01 V9
#> 10 4.732393e-02 V10
#> 11 -3.465666e-02 V11
#> 12 2.002576e-01 V12
#> 13 -4.510807e-02 V13
#> 14 -2.545680e-01 V14
#> 15 -1.679573e-03 V15
#> 16 7.318178e-02 V16
#> 17 6.446624e-02 V17
#> 18 1.999834e-01 V19
#> 19 1.120717e-01 V20
您在没有感兴趣的列的情况下循环遍历数据框。当您决定使用 lapply 时,您最终会得到一个列表,您可以使用 stack
.