1

With the code below I have created a correlation matrix. The code below just creates a matrix for all of the data, regardless of treatment. However, a column in my data is treatment. I would like to make two different matrices (one for each treatment type). My treatment a categorical value in column 6 'Treat'. Columns 10 through 44 I would like to create a matrix for.

correlations <- cor(Plants[,c(10:44)], use="pairwise.complete.obs", method="pearson")
correlations<-as.matrix(correlations) 
user1977802
  • 313
  • 6
  • 10
  • 18

2 Answers2

1

If you have just two treatment categories then you can make two separate analysis for each level (selecting the data just for particular level). Just replace names Treatment1 and Treatment2 with actual names of your treatments.

kor1<-cor(Plants[Plants$Treat=="Treatment1",c(10:44)], use="pairwise.complete.obs", method="pearson")
kor2<-cor(Plants[Plants$Treat=="Treatment2",c(10:44)], use="pairwise.complete.obs", method="pearson")
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
1

You could use by to apply a function to different subsets of your data.

by(Plants[10:44], Plants["Treat"],
   cor, use = "pairwise.complete.obs", method = "pearson")
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168