Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a matrix element without the column name in R?

Tags:

r

This seems to be simple but I can't find the answer. I combine two vectors using cbind().

> first = c(1:5)
> second = c(6:10)
> values = cbind(first,second)

When I want to retrieve a single element using values[1,2] I always get the column name in addition to the actual element.

> values[1,2]

second
6

How can I get the value without the column name?

I know I can remove the column names in the matrix like in this post: How to remove column names from a matrix in R? But how can I leave the matrix as is and only get the value I want?

like image 409
swbandit Avatar asked Nov 28 '15 20:11

swbandit


People also ask

How do I remove column names from a matrix in R?

To remove the row names or column names from a matrix, we just need to set them to NULL, in this way all the names will be nullified.

How do I remove column names in R?

The most easiest way to drop columns is by using subset() function. In the code below, we are telling R to drop variables x and z. The '-' sign indicates dropping variables. Make sure the variable names would NOT be specified in quotes when using subset() function.

Can a matrix have column names in R?

We use colnames() function for renaming the matrix column in R. It is quite simple to use the colnames() function. If you want to know more about colnames() function, then you can get help about it in R Studio using the command help(colnames) or ? colnames().


1 Answers

We can use unname

unname(values[1,2])
#[1] 6

Or as.vector

as.vector(values[1,2])

You can use the [[ operator to extact a single element,

values[[1,2]]
# [1] 6
like image 70
akrun Avatar answered Sep 24 '22 05:09

akrun