Reputation: 1011
I inherited someone's R code, and one of the key results of the code is a structure that R identifies only as "numeric" by class. When I print it out as.matrix, here's what it looks like:
[,1]
1 3.062500e-08
63 2.691366e-03
36 4.202501e-09
41 1.247481e-09
82 1.040101e-08
63 2.691366e-03
63 2.691366e-03
58 2.700624e-09
...
What I need to extract are those indices on the left - for example, for the first element the index is 1, for the second one it's 63, and so on. I can't seem to figure out how to do it. I've tried to get it by using 'row' (does not work, always gives me 1), by turning this structure into a data frame and looking for colnames, but nothing works. Could someone please help?
Upvotes: 0
Views: 75
Reputation: 270348
The object m
in the Note at the end has a class which is matrix
and its mode is numeric
. It is a one column matrix with row names which seems to be what you have.
With that we can use rownames
to get the row names. The row names are always character so you might want to convert them to numeric as we show below.
as.numeric(rownames(m))
## [1] 1 63 36 41 82 63 63 58
We assume that the input is given as shown here:
m <- matrix(c(3.0625e-08, 0.002691366, 4.202501e-09, 1.247481e-09,
1.040101e-08, 0.002691366, 0.002691366, 2.700624e-09), 8,
dimnames = list(c("1", "63", "36", "41", "82", "63", "63", "58"), NULL))
giving:
> m
[,1]
1 3.062500e-08
63 2.691366e-03
36 4.202501e-09
41 1.247481e-09
82 1.040101e-08
63 2.691366e-03
63 2.691366e-03
58 2.700624e-09
> class(m)
[1] "matrix"
> mode(m)
[1] "numeric"
Upvotes: 3