I'm looking for a way to change labels/dimnames on imported data used for metaSEM. Specifically, I am looking a way to label each study and change the dim names to actually represent the variables under investigation. The easiest way to illustrate what I'd like to do is walking through an example.
When using the following code to import two correlation matrices
cat("1.0\n0.3 1.0\n0.4 0.5 1.0\n1.0\nNA NA\n0.4 NA 1.0",
file="lowertriangle.dat", sep="")
my.lowertri <- readLowTriMat(file = "lowertriangle.dat", no.var = 3)
my.lowertri
I get the following results
$`1`
x1 x2 x3
x1 1.0 0.3 0.4
x2 0.3 1.0 0.5
x3 0.4 0.5 1.0
$`2`
x1 x2 x3
x1 1.0 NA 0.4
x2 NA NA NA
x3 0.4 NA 1.0
What I'd like to do is name the correlation by the study name ('1' = 'Johnson et al (2010)) and name the actual variables instead of using the x* defaults (e.g., x1 = "conscientiousness").
I'm relatively new at this, so I'm hoping that I'm just missing something really simple.
Thanks!
Welcome to the site. This should probably be on Stackoverflow.
Look at the functions names
, rownames
and colnames
. Remember that you are dealing with a list, as the function class
will tell you.
An easy solution could be:
a <- matrix(sample(c(0,1), 9, replace = T), 3, 3)
b <- matrix(sample(c(0,1), 9, replace = T), 3, 3)
example <- list("1" = a,"2"= b)
example
names(example) <- c("Johnson et al (2010)","Einstein (1905)")
rownames(example[[1]]) <- c("Conscientiousness","Conscientiousness","Conscientiousness")
colnames(example[[2]]) <- c("Conscientiousness","Conscientiousness","Conscientiousness")
If you use the search bar, you can probably find a lot of resources that point to how change the name attributes of lists.