Create a simple DataFrame
data <- data.frame(
- Turtle = c("Sp1", "Sp2", "Sp3", "Sp4"),
- Lake = c(3, 2, 6, 2),
- River = c(12, 2, 9, 8),
- Igapo = c(1, 3, 11, 1)
- )
Display the DataFrame
print(data)
Turtle Lake River Igapo
1 Sp1 3 12 1
2 Sp2 2 2 3
3 Sp3 6 9 11
4 Sp4 2 8 1Assuming your DataFrame is called 'data'
species <- data$Turtle
environments <- colnames(data)[2:4] # Select environment columnsInitialize a matrix to store correlations
correlations <- matrix(NA, nrow = length(species), ncol = length(environments))
rownames(correlations) <- species
colnames(correlations) <- environmentsCalculate Pearson correlation for each combination
for (i in 1:length(species)) {
- for (j in 1:length(environments)) {
-
correlations[i, j] <- cor(data[[environments[j]]], data[[environments[i]]], method = "pearson")
- }
- }
Error in cor(data[[environments[j]]], data[[environments[i]]], method = "pearson") :
forneça conjuntamente 'x' e 'y' ou algo semelhante a uma matriz 'x'
Display the correlation matrix
print(correlations)
Lake River Igapo
Sp1 1.0000000 0.38844208 0.92465840
Sp2 0.3884421 1.00000000 0.01669684
Sp3 0.9246584 0.01669684 1.00000000
Sp4 NA NA NA