apply
obbliga i dati a una matrice prima di chiamare la funzione su ogni colonna. as.matrix(df_AB)
ottiene rownames non nulli, mentre as.matrix(df_ab)
non lo fa:
> str(as.matrix(df_ab))
int [1:5, 1:2] 1 2 3 4 5 6 5 4 3 2
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:2] "a" "b"
> str(as.matrix(df_AB))
int [1:5, 1:2] 1 2 3 4 5 6 5 4 3 2
- attr(*, "dimnames")=List of 2
..$ : chr [1:5] "1" "2" "3" "4" ...
..$ : chr [1:2] "a" "b"
Quindi, quando si apply
subsest una colonna di df_AB
, si ottiene un vettore di nome, che non è identico a un vettore senza nome.
apply(df_AB, 2, str)
Named int [1:5] 1 2 3 4 5
- attr(*, "names")= chr [1:5] "1" "2" "3" "4" ...
Named int [1:5] 6 5 4 3 2
- attr(*, "names")= chr [1:5] "1" "2" "3" "4" ...
NULL
subset
sceglie righe utilizzando un vettore logico per il valore di i
, ed assomiglia sottoinsiemi un data.frame con un valore non mancante per i
cause questa differenza di attributo row.names
:
> str(as.matrix(df[1:5, 1:2]))
int [1:5, 1:2] 1 2 3 4 5 6 5 4 3 2
- attr(*, "dimnames")=List of 2
..$ : chr [1:5] "1" "2" "3" "4" ...
..$ : chr [1:2] "a" "b"
> str(as.matrix(df[, 1:2]))
int [1:5, 1:2] 1 2 3 4 5 6 5 4 3 2
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:2] "a" "b"
È possibile visualizzare la differenza nei data.frames stessi utilizzando la funzione .Internal(inspect(x))
. Si noti che quando manca i
, i valori dell'attributo row.names
sono entrambi negativi, ma il secondo valore è positivo se si imposta un sottoinsieme con uno i
mancante.
> .Internal(inspect(df[, 1:2]))
@26e8500 19 VECSXP g0c2 [OBJ,NAM(2),ATT] (len=2, tl=0)
@1740aa0 13 INTSXP g0c3 [MARK,NAM(2)] (len=5, tl=0) 1,2,3,4,5
@1745f28 13 INTSXP g0c3 [MARK,NAM(2)] (len=5, tl=0) 6,5,4,3,2
ATTRIB:
@27f31e8 02 LISTSXP g0c0 []
TAG: @13958f8 01 SYMSXP g0c0 [MARK,LCK,gp=0x4000] "names" (has value)
@26e8570 16 STRSXP g0c2 [NAM(2)] (len=2, tl=0)
@154ed28 09 CHARSXP g0c1 [MARK,gp=0x61] [ASCII] [cached] "a"
@171d128 09 CHARSXP g0c1 [MARK,gp=0x61] [ASCII] [cached] "b"
TAG: @1395dc8 01 SYMSXP g0c0 [MARK,LCK,gp=0x4000] "class" (has value)
@2648f88 16 STRSXP g0c1 [MARK,NAM(2)] (len=1, tl=0)
@1430aa8 09 CHARSXP g0c2 [MARK,gp=0x61,ATT] [ASCII] [cached] "data.frame"
TAG: @139f6e0 01 SYMSXP g0c0 [MARK,LCK,gp=0x4000] "row.names" (has value)
@2a588f8 13 INTSXP g0c1 [] (len=2, tl=0) -2147483648,-5
> .Internal(inspect(df[1:5, 1:2]))
@26e8260 19 VECSXP g0c2 [OBJ,NAM(2),ATT] (len=2, tl=0)
@2a56e70 13 INTSXP g0c3 [NAM(1)] (len=5, tl=0) 1,2,3,4,5
@2a56d98 13 INTSXP g0c3 [NAM(1)] (len=5, tl=0) 6,5,4,3,2
ATTRIB:
@27f9ae8 02 LISTSXP g0c0 []
TAG: @13958f8 01 SYMSXP g0c0 [MARK,LCK,gp=0x4000] "names" (has value)
@26e8458 16 STRSXP g0c2 [NAM(2)] (len=2, tl=0)
@154ed28 09 CHARSXP g0c1 [MARK,gp=0x61] [ASCII] [cached] "a"
@171d128 09 CHARSXP g0c1 [MARK,gp=0x61] [ASCII] [cached] "b"
TAG: @139f6e0 01 SYMSXP g0c0 [MARK,LCK,gp=0x4000] "row.names" (has value)
@22a7818 13 INTSXP g0c1 [] (len=2, tl=0) -2147483648,5
TAG: @1395dc8 01 SYMSXP g0c0 [MARK,LCK,gp=0x4000] "class" (has value)
@2648f88 16 STRSXP g0c1 [MARK,NAM(2)] (len=1, tl=0)
@1430aa8 09 CHARSXP g0c2 [MARK,gp=0x61,ATT] [ASCII] [cached] "data.frame"
Come Roland pointed out in his comment, questo è più facile vedere utilizzando la funzione .row_names_info
.
> .row_names_info(df_ab, type=1)
[1] -5
> .row_names_info(df_AB, type=1)
[1] 5
Ciò che questi valori medi è spiegata in ?.row_names_info
:
type: integer. Currently ‘type = 0’ returns the internal
‘"row.names"’ attribute (possibly ‘NULL’), ‘type = 2’ the
number of rows implied by the attribute, and ‘type = 1’ the
latter with a negative sign for ‘automatic’ row names.
Domanda interessante e utile! –