Bug 15638 - Extracting high dimensional structures from data frames
Summary: Extracting high dimensional structures from data frames
Status: NEW
Alias: None
Product: R
Classification: Unclassified
Component: Wishlist (show other bugs)
Version: R 3.0.2
Hardware: All All
: P5 enhancement
Assignee: R-core
Depends on:
Reported: 2014-01-13 22:32 UTC by Jeroen Ooms
Modified: 2014-01-13 22:32 UTC (History)
0 users

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Jeroen Ooms 2014-01-13 22:32:58 UTC
A powerful feature of data frames is to add elements with high dimensional structures. These appear frequently in the data that I work with, and I was happily surprised that R perfectly handles these for 2 dimensional structures. However the behavior of "[" seems somewhat unexpected for elements with more than 2 dimensions.

Below example code with comments explaining the issue.

#To add high dimensional structures to a dataframe, the first dimension (nrow) has to match
mymatrix <- matrix(1:6, 2)
mydf <- data.frame(x = c("foo", "bar"))
dim(mymatrix)[1] == dim(mydf)[1]
mydf$y <- mymatrix
mydf$z <- as.data.frame(mymatrix)

#works as expected when length(dim) == 2
identical(mydf[1,"y"], mydf$y[1,, drop=FALSE])
identical(mydf[1,"z"], mydf$z[1,, drop=FALSE])

# The same way we can add high dimensional arrays
myarray <- array(1:12, dim=c(2,2,3))
dim(myarray)[1] == dim(mydf)[1]
mydf$k <- myarray

# However, "[" doesn't behave as expected
mydf[1, "k"]

# I would have expected this to be TRUE
identical(mydf[1,"k"], mydf$k[1,,, drop=FALSE])