Reputation: 121
I dont have much knowledge in R. I have a .txt file with a correlation matrix which was previously created from long records.
the text there in the file looks something like:
"15075060" "15085030" "15085040"
"15075060" 1 0.441716695007761 0.433807683928689
"15085030" 0.441716695007761 1 0.477591938543259
"15085040" 0.433807683928689 0.477591938543259 1
This is a representative example because the real matrix is much bigger. The numbers in the quotation marks are the sources that were correlated. I read the data using read.table to create a data frame and then i convert it into a matrix (called matto) with:
mattox =matrix(as.numeric(unlist(matto)),nrow=nrow(matto))
and I obtain a matrix like this:
>mattox
[,1] [,2] [,3]
[1,] 1.0000000 0.4417167 0.4338077
[2,] 0.4417167 1.0000000 0.4775919
[3,] 0.4338077 0.4775919 1.0000000
as an option 2, if I convert it into a matrix using:
as.matrix(sapply(matto, as.numeric))
then i obtain a matrix like this:
> matto
X.15075060 X.15085030 X.15085040
15075060 1.0000000 0.4417167 0.4338077
15085030 0.4417167 1.0000000 0.4775919
15085040 0.4338077 0.4775919 1.0000000
although I dont know why I get those X before the numbers at the column heads
when I try to plot this correlations using the function corrplot i obtain something like this for the matrix mattox:
corrplot(mattox, type="upper")
but the problem is that i dont see here the head names of the columns and rows (numbers in quotation marks from the .txt file). And for the other matrix (matto) i obtain an error when i try to use corrplot, the error says:
Error in matrix(if (is.null(value)) logical() else value, nrow = nr, dimnames = list(rn, :
length of 'dimnames' [2] not equal to array extent
I would like to obtain a graphic just like the one I obtained but with the names of columns and rows instead of numbers 1,2,3... something like the next graph, which I found online for othe case:
how can I fix this?
Upvotes: 1
Views: 6548
Reputation: 2091
You can skip those steps and just coerce it to a matrix when you read it, and should already be numeric. It prepends the names with an x
due to those names being duplicates. You can specify colnames
though.
df <- as.matrix(read.table("location/of/text.txt", row.names = 1))
colnames(df) <- c("15075060", "15085030", "15085040")
str(df) # check the structure, it's numeric so we're good
num [1:3, 1:3] 1 0.442 0.434 0.442 1 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:3] "15075060" "15085030" "15085040"
..$ : chr [1:3] "15075060" "15085030" "15085040"
corrplot(df, type = "upper")
Upvotes: 1