I would like to correlate two variables and have the output reported separately for levels of a third variable.
My data are similar to this example:
var1 <- c(7, 8, 9, 10, 11, 12)
var2 <- c(18, 17, 16, 15, 14, 13)
categories <- c(1, 2, 3, 1, 2, 3)
And I want to correlate var1 with var2 within the categories, such that the results would show the correlation of the values of var1 and var2 for category 1 separately from category 2 and category 3.
In SAS, I would do:
PROC CORR DATA=x;
BY CATEGORY
VAR VAR1
WITH VAR2;
RUN;
You can put your records into a data.frame and then split by the cateogies and then run the correlation for each of the categories.
This can look prettier with the
dplyr
library