[英]Calculate correlations between data.frame columns and assign output to list
[英]Correlations between the selected column and the rest of the data.frame in R
我的數據大約有 270 列,其中 160.000 個主要是非數字觀察。
我需要找到列之間的模式和依賴關系。 例如,我需要將“材料”列與其他列相關聯。
Material | Name | Country | Vehicle
----------------------------------------------
Bricks | John | A | Car
Bricks | John | A | Car
Bricks | John | A | Motorcycles
Bricks | John | B | Motorcycles
Concrete | Bill | B | Car
Concrete | Bill | B | Car
Concrete | Bill | B | Car
Concrete | Bill | A | Car
我想要的結果是:
Name - 100%
Country - 75%
Vehicle - 50%
我試過:
library("GoodmanKruskal")
Cor_matrix<- GKtauDataframe(df)
plot(Cor_matrix)
但得到:表中的錯誤(x,y,useNA = includeNA):嘗試使用> = 2 ^ 31個元素制作表格
或者:
library("corrr")
df %>% correlate() %>% focus(Material)
stats::cor(x = x, y = y, use = use, method = method) 中的錯誤:'x' 必須是數字
所以我正在尋找可以處理非數字的包和代碼示例。 提前謝謝了。
您的代碼使用函數GKtauDataframe
,該函數嘗試同時計算所有 270 x 270 組合的指標。 那太多了。
但是,正如您所提到的,您希望將一列與所有其他列進行比較。 這應該是可行的,並且不需要那么多內存。 函數GKtau
在一對列之間執行此操作:
GKtau(df[, 1], df[, 2])
要針對所有其他列獲取第一列的值,只需調用:
lapply(df[, -1], GKtau, df[, 1])
當然,您可以使用以下方法優化輸出:
sapply(df[, -1], function(di) GKtau(df[, 1], di)$tauxy)
這使得輸出方式更加緊湊。
df
中的列是factor
類型,則需要先轉換為數字。df[] <- Map(as.numeric,df)
除此以外
df[] <- Map(function(v) as.numeric(factor(v)),df)
df %>% correlate() %>% focus(Material)
# A tibble: 3 x 2
rowname Material
<chr> <dbl>
1 Name -1
2 Country 0.5
3 Vehicle -0.577
數據
df <- structure(list(Material = structure(c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L), .Label = c("Bricks", "Concrete"), class = "factor"),
Name = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("Bill",
"John"), class = "factor"), Country = structure(c(1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L), .Label = c("A", "B"), class = "factor"),
Vehicle = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("Car",
"Motorcycles"), class = "factor")), class = "data.frame", row.names = c(NA,
-8L))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.