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.
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).
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With