All objects
can have names, these are used to write readable code, “names()” function is
used to set names to an object.
Setting names to elements of vector
> age <- c(25, 27, 29) > names(age) NULL > > names(age) <- c("Hari Krishna", "Rama Krishna", "Joel") > > age Hari Krishna Rama Krishna Joel 25 27 29
In the above
example, I given a name to each elements in the vector age.
25 – Hari Krishna
27 – Rama Krishna
29 -
Joel
After
setting names, when you print the vector age, it displays names over the
members of the vector age.
Setting names to elements of list
> age <- list(Hari=25, Rama=27, Joel=26) > age $Hari [1] 25 $Rama [1] 27 $Joel [1] 26
When I print
list age, it prints names and value associated with the name.
Setting names to matrices
We can set
names to columns and rows of a matrix.
> matrix1 <- matrix(1:4, nrow=2, ncol=2) > > dimnames(matrix1) <- list(c("row1", "row2"), c("col1", "col2")) > > matrix1 col1 col2 row1 1 3 row2 2 4
No comments:
Post a Comment