Well, if you can live with names intact:
> h <- structure(42, foo=4, bar=2)
> unlist(attributes(h))
foo bar
4 2
Otherwise (which is actually faster!),
> unlist(attributes(h), use.names=FALSE)
[1] 4 2
Performance is as follows:
system.time( for(i in 1:1e5) unlist(attributes(h)) )
system.time( for(i in 1:1e5) unlist(attributes(h), use.names=FALSE) )
system.time( for(i in 1:1e5) as.integer(paste(attributes(h))) )
system.time( for(i in 1:1e5) as.vector(sapply(names(attributes(h)),
function(x) attr(h, x))) )
Tommy source
share