0

我仍然是 R 的新手,所以这是我的问题。

我有一个名为 dataset3 的数据框。

structure(list(Varietes = c("Georgia", "Georgia", "Georgia", "Georgia", "Georgia", "Georgia", "Georgia", "Ruby", "Ruby", "Ruby", "Ruby", "Ruby", "Ruby", "Ruby", "Abelastone", "Abelastone", "Abelastone", "Abelastone", "Abelastone"), ligne.rep = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Pied = c(1, 2, 3, 4, 5, 7, 8, 1, 3, 4, 5, 6, 7, 8, 1, 2, 3, 6, 7), Date.floraison.mâle = structure(c(1627689600, 1627430400, 1627689600, 1627344000,1627516800,1627603200, 1627689600, 1627516800, 1627776000, 1627603200, 1627344000,1627516800, 1627689600, 1627776000, 1627516800, 1627862400, 1627776000, 1627948800, 1628035200), tzone = "UTC", class = c("POSIXct", "POSIXt")), Date.floraison.femelle = structure(c(1627776000, 1627948800, 1627862400, 1627948800, 1628121600, 1628035200, 1627862400, 1627862400, 1628035200,1628121600, 1627862400, 1627948800, 1628380800, 1628121600, 1628553600, 1629158400, 1628640000, 1629849600, 1629158400), tzone = "UTC", class = c("POSIXct", "POSIXt")), ASIi = c(1, 6, 2, 7, 7, 5, 2, 4, 3, 6, 6, 5, 8, 4, 12, 15, 10, 22, 13), Hauteur.des.pieds = c(200, 210, 205, 215, 217, 207, 205, 208, 206, 215, 216, 212, 220, 215, 230, 228, 226, 240, 233), Hauteur.des.soies = c(100, 104, 102, 110, 108, 103, 102, 105, 105, 110, 112, 108, 113, 110, 123, 118, 116, 124, 122)), row.names = c(NA, -19L), class = c("tbl_df", "tbl", "data.frame"), na.action = structure(c(`6` = 6L, `10` = 10L, `20` = 20L, `21` = 21L, `24` = 24L), class = "omit"))

这是虚假数据。我现在正在尝试编写一个对未来非常有用的脚本。

我想以最简单的方式询问我的数据以获得相关性,例如“ASIi”和“Hauteur des pieds”,但对于我的专栏“Varietes”的每个品种。在我的数据集 3 中,我得到了 3 个品种:Georgia、Abelastone、Ruby。

因为在我的真实数据集中,我将有 50 个品种,如果我必须隔离每个品种,这并不容易。

有没有更简单的方法来做到这一点?非常感谢

4

1 回答 1

0

获得相同变量的相关性,但对于每个品种

我们可以遍历两个品种的每一种组合:

comb.var = combn(unique(dataset3$Varietes), 2) #Get the combinations

correlations = list() #Create empty list to store values
cols = c("Pied", "ASIi", "Hauteur.des.pieds", "Hauteur.des.soies") #Define the columns that you want to get correlations for

for(i in 1:ncol(comb.var)){
  name = paste(comb.var[,i], collapse="/")
  correlations[[name]] = numeric() #Create a empty vector for the combination
  for(j in cols){
    correlations[[name]][j] = cor(dataset3[dataset3$Varietes==comb.var[1,i],j],
                                  dataset3[dataset3$Varietes==comb.var[2,i],j])}} #Fill the vector with each correlation

OBS:您的示例数据对 的观察较少Abelastone,如果在您的真实数据集中,每个品种都有相同数量的 obs,这不会是一个问题,否则我可以更概括一下代码。输出:

> correlations
$`Georgia/Ruby`
             Pied              ASIi Hauteur.des.pieds Hauteur.des.soies 
        0.9787781         0.1828599         0.1065409         0.3239701 

$`Georgia/Abelastone`
             Pied              ASIi Hauteur.des.pieds Hauteur.des.soies 
        0.9048705         0.6451144         0.5370488         0.3387600 

$`Ruby/Abelastone`
             Pied              ASIi Hauteur.des.pieds Hauteur.des.soies 
       0.93982306        0.05192741        0.44198418        0.21908902 

为了获得不同变量的相关性,对于每个品种

我们只是颠倒循环的逻辑:

comb.col = combn(cols, 2)

for(i in 1:ncol(comb.col)){
  name = paste(comb.col[,i], collapse="/")
  correlations[[name]] = numeric()
  for(j in unique(dataset3$Varietes)){
    correlations[[name]][j] = cor(dataset3[[comb.col[1,i]]][dataset3$Varietes==j],
                                  dataset3[[comb.col[2,i]]][dataset3$Varietes==j])}}

输出:

> correlations
$`ASIi/Hauteur.des.pieds`
   Georgia       Ruby Abelastone 
 0.9340590  0.8795120  0.8174805 

$`ASIi/Hauteur.des.soies`
   Georgia       Ruby Abelastone 
 0.8849296  0.8489775  0.5125693 

$`Hauteur.des.pieds/Hauteur.des.soies`
   Georgia       Ruby Abelastone 
 0.9519358  0.9789882  0.8293315 
于 2021-06-02T14:35:54.707 回答