简体   繁体   中英

row.name using `structure` function as c(NA, *integer*)

Does anyone know why when I run this:

row.names(structure(list(speed = c(4, 7), dist = c(2, 22)), 
                    row.names = c(NA, 2L), class = "data.frame"))

I get this:

# "1" "2"

and not c(NA, 2L) ? I mean what row.names argument in structure exactly does to the argument?

I came across this when I tried to use dput to see the structure of some dataframes. eg

dput(cars)

And I noticed the row.names argument in it, which is: c(NA, -50L) .

c(NA, n) is how data frames internally store the row names in the common case of 1:n so as to save space and processing time. This is not supposed to be accessible to the user who is to regard it as "1", "2", ... so the accessor functions translate it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM