I create a data frame dfrm with one column, and set the row names as so:
v1 = c(1,2,3)
dfrm <- data.frame(v1)
row.names(dfrm) <- c("AD","BP","CD")
dfrm
v1
AD 1
BP 2
CD 3
I can access elements by row name and index:
dfrm$v1[1]
[1] 1
I can access elements by row name and component name in quotes:
dfrm["AD","v1"]
[1] 1
But why can’t I access elements by row name and component name?
dfrm$v1["AD"]
[1] NA
The answer is that vectors don’t have rownames although they can have names.
When you access a column as a list item, R does not take the additional step of passing along the rownames to the names of the vector:
Note that this is probably a good thing, as the cases where this is helpful are limited, and the overhead to copy the names every time a data.frame has a column pulled out likely not worth it.
If you want to copy them yourself: