R: to which objects does the names () attribute apply?

4

I'm a little confused by the role of the names() attribute on different objects.

In addition to the normal use in data.frames, I see examples of help(names) that can also be used for vectors (both numeric and string) and lists. But for arrays names(M) returns NULL . Here are some examples:

# vetor numérico
names(islands)
islands

#vertor textual
a <- c("a","b")
names(a) <- c("Na","Nb")
a

#listas
z <- list(a = 1, b = "c", c = 1:3)
names(z)

#data.frames
x <-  data.frame(V1=c(1,2,3),V2=c(3,2,1))
names(x)
x

#matrizes:
y <- as.matrix(x)
names(y)
y

Can Factors also have names? Why only arrays do not have "names"? colnames() and rownames() are attributes other than names() ?

    
asked by anonymous 25.09.2014 / 21:00

2 answers

3

Arrays can have names as well. Almost everything can have names in R . The simplest way to think about these issues is as follows: names refers to the element name of the object. E rownames and colnames are names of the dimensions of the object.

Vectors, Arrays and Arrays

For example, in a vector, names gives a name to each element of the vector.

x <- 1:10
names(x) <- paste0("elemento", 1:10)
x
 elemento1  elemento2  elemento3  elemento4  elemento5  elemento6  elemento7  elemento8 
         1          2          3          4          5          6          7          8 
 elemento9 elemento10 
         9         10 

And now you can use the name to select the element:

x["elemento1"]
elemento1 
        1 

An array (or an array) is nothing more than a vector with one more attribute: dimensions . Let's transform the x vector above into a 2 x 5 matrix. You can now name all array elements with names, as well as name the dimensions, that is, rows and columns, using rownames and colnames respectively:

dim(x) <- c(2, 5)
names(x) <- paste0("elemento", 1:10)
rownames(x) <- paste0("linha", 1:2)
colnames(x) <- paste0("coluna", 1:5)
x
       coluna1 coluna2 coluna3 coluna4 coluna5
linha1       1       3       5       7       9
linha2       2       4       6       8      10
attr(,"names")
 [1] "elemento1"  "elemento2"  "elemento3"  "elemento4"  "elemento5"  "elemento6" 
 [7] "elemento7"  "elemento8"  "elemento9"  "elemento10"

Now you can subset the names of both elements and dimensions:

x["elemento10"]
elemento10 
        10 
x[,"coluna5"]
linha1 linha2 
     9     10 

Matrices have only two dimensions. For more than two dimensions we have the array. Let's transform the previous array into an array 2 X 5 x 1. With the array you can continue using rownames and colnames to name the first and second dimension. To name the third dimension, you will use dimnames :

dim(x) <- c(2, 5, 1)
names(x) <- paste0("elemento", 1:10)
rownames(x) <- paste0("linha", 1:2)
colnames(x) <- paste0("coluna", 1:5)
dimnames(x)[[3]] <- "terceira dimensão"
x
, , terceira dimensão

       coluna1 coluna2 coluna3 coluna4 coluna5
linha1       1       3       5       7       9
linha2       2       4       6       8      10

attr(,"names")
 [1] "elemento1"  "elemento2"  "elemento3"  "elemento4"  "elemento5"  "elemento6"  "elemento7"  "elemento8" 
 [9] "elemento9"  "elemento10"

The dimanames is the primitive function for dimensions. So obviously you could name both rows and columns also with dimnames .

# Nomeando linhas e colunas usando dimnames
dimnames(x)[[1]] <- paste0("linha", 1:2)
dimnames(x)[[2]] <- paste0("coluna", 1:5)

Lists and data.frames

If you create the list z <- list(a = 1, b = "c", c = 1:3) , note that its elements have the names a , b and c , and this is what the names return.

names(z)
[1] "a" "b" "c"

The data.frame is a list too, with a row.names attribute and with the restriction that all elements must be the same size. Let's turn z into a data.frame:

z <- as.data.frame(z)
  a b c
1 1 c 1
2 1 c 2
3 1 c 3

It is now clear why no data.frame names is equal to colnames . This is because the data.frame is nothing more than a list and the columns are the elements of the list.

Finally, factors are vectors, and can have names normally.

    
26.09.2014 / 01:04
2

Your question is related to data structures.

See Hadley's book: link

names is an attribute type (which is the metadata in R), which is used to give names to elements of a vector. So you can assign names using names , both for atomic vectors (usually built with c() ) and for lists (usually built with list() ). Thus, an object with class data.frame is a list, which in turn is a vector, so it can receive the names attribute.

Arrays, however, are a special case of arrays. An array (usually constructed with array() ) is a vector that has the dim attribute.

Thus, you can assign names to a array or matrix , but this means assigning a name to each element, not the columns or rows. For example:

x <- matrix(c(1,2,3,4,5,6), nrow=2)
attributes(x)
names(x) <- c('a', 'b', 'c') # atribui nomes aos três primeiros elementos do array
attributes(x)
colnames(x) <- c('a', 'b', 'c') # atribui nomes das colunas do array (dimensão 2)
attributes(x)

dim(x) <- NULL # transforma a matriz em vetor atômico (os nomes são dropados)
names(x) <- c('a', 'b', 'c') # atribui nomes aos três primeiros elementos do vetor
attributes(x)

Although it is possible to assign names to each element of a array , this may not be useful, which is why the generalizations rownames and colnames exist.

I think you can now answer your questions.

  • A factor is a vector of integers with the attributes class and levels . Therefore, since it is a vector, it can have names that are. See for example x <- factor(c(a='A',b='B',c='C'))
  • Matrices are not born with names probably because it is useless to name each element of the array. You may notice that creating an array from a named array, for example: matrix(c(a=1,b=2,c=3,d=4,e=5,f=5), nrow=2) throws out the names attribute. However, arrays can receive names yes.
  • colnames and rownames are in fact attributes other than names , and are part of the dimnames list.
26.09.2014 / 01:23