I have this dataframe in r (link) (Example rows and columns below)
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
I would like to add a new variable, df$cor, in which the value in each row is the result of a correlation. The correlation should be between two vectors: (1) the column whose colname corresponds to the value of the variable df$FocalID in that row, and (2) the column whose colname corresponds to the value of the variable df$Mother in that row.
If the vector correspondent to the column that matches the mother's name is absent (either because the mother is not known (NA in df$Mother) or absent from colnames), the correlation should produce an NA.
I have tried the following code:
df$cor <- cor(df[, colnames(df) %in% df$FocalID], df[, colnames(df) %in% df$Mother])
However, the result doesn't seem right. Any idea?