0
votes

I am very new at R and am struggling to create a matrix. My eventual goal is to produce a variance covariance matrix comparing 6 numeric variables (columns) by groups. I have 2187 rows of data, which are divided among to several hundred groups. I tried to create a matrix with variations of the following, using the help(matrix) information. This gives me a matrix that is the correct size, but filled with x,y information based on the structure of the database:

matrix(data = PhenoM, nrow = 2187, ncol = 6, byrow = FALSE, dimnames = NULL)

    [,1]       [,2]      [,3]       [,4]       [,5]       [,6]

[,1] factor,2187 Integer,2187 Integer,2187 Numeric,2187 Numeric,2187 .factor,2187

[,2] factor,2187 Numeric,2187 Numeric,2187 Numeric,2187 factor,2187 Integer,2187

[,3] factor,2187 Numeric,2187 Numeric,2187 Numeric,2187 factor,2187 Numeric,2187

I'm also not sure how to instruct R that the 6 columns of information I want in the matrix are the last 6 of 13 (read right to left). Thank you for your help!

1

1 Answers

0
votes

Subset your matrix (or data.frame) and use cov in base R to create a covariance matrix:

cov(PhenoM[1:2187,8:13])

Notice I have subset PhenoM by the last six columns in a 13 column data structure (i.e., I have selected columns 8 through 13). I recommend learning about R's basic data structures and how to subset them.