2

私はこのデータフレームをr(リンク)に持っています(以下の行と列の例)

FocalID     Mother   Adelaide Asimov Austen Brazzaville Lusaka Kinshasa
Adelaide    HalfEar    0      0.0380 0.0417      0.0366 0.0278   0.0385
Asimov      Lusaka     0.0380 0      0.0845      0.0357 0.169    0.0641
Austen      Kinshasa   0.0417 0.0845 0           0.0526 0.0952   0.0411
Brazzaville NA         0.0366 0.0357 0.0526      0      0.0395   0.0488

各行の値が相関の結果である新しい変数 df$cor を追加したいと思います。相関関係は 2 つのベクトル間である必要があります: (1) colname がその行の変数 df$FocalID の値に対応する列、および (2) colname がその行の変数 df$Mother の値に対応する列.

母親の名前に一致する列に対応するベクトルが存在しない場合 (母親が不明なため (df$Mother の NA)、または colnames に存在しないため)、相関は NA を生成する必要があります。

次のコードを試しました:

df$cor <- cor(df[, colnames(df) %in% df$FocalID], df[, colnames(df) %in% df$Mother])

しかし、結果は正しくないようです。何か案が?

4

1 に答える 1