To create a correlation matrix with certain variables in R, you can use the cor()
function along with the subset()
function.
Here's an example code snippet:
main.r278 chars14 lines
In this example, we create a sample data frame df
with 4 variables. We then use subset()
to select only var1
and var2
, and pass that subset to the cor()
function to compute the correlation matrix. Finally, we print the resulting correlation matrix.
You can modify the subset()
function to include any variables you're interested in, and the resulting corr_matrix
will only include correlations between those variables.
gistlibby LogSnag