Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R suppress names when displaying or printing a named vector

Tags:

r

I am wondering if, given a named vector, if it is possible to print (or display in the R console) only the values of the vector without deleting the names.

# EXAMPLE
v <- (1:5)
names(v) <- LETTERS[1:5]

print(v)
# RESULT: 
#  A B C D E 
#  1 2 3 4 5 

# RESULT I AM SEEKING 
#  [1] 1 2 3 4 5

I am able to get the result I am looking for using the following function. However, is there a better or more direct way of printing only the values of a named vector?

print.n <- function (obj) {
  names(obj) <- NULL
  print(obj)
}

print.n(v)
#  [1] 1 2 3 4 5

Thanks.

like image 690
Ricardo Saporta Avatar asked Nov 09 '12 21:11

Ricardo Saporta


People also ask

How do I remove a name from a vector in R?

To assign names to the values of vector, we can use names function and the removal of names can be done by using unname function. For example, if we have a vector x that has elements with names and we want to remove the names of those elements then we can use the command unname(x).

How do I remove a name from R?

In R, the easiest way to remove columns from a data frame based on their name is by using the %in% operator. This operator lets you specify the redundant column names and, in combination with the names() function, removes them from the data frame. Alternatively, you can use the subset() function or the dplyr package.


1 Answers

Try unname():

R> v <- (1:5); names(v) <- LETTERS[1:5]
R> print(v)
A B C D E 
1 2 3 4 5 
R> print(unname(v))
[1] 1 2 3 4 5
R> 
like image 91
Dirk Eddelbuettel Avatar answered Oct 14 '22 14:10

Dirk Eddelbuettel