简体   繁体   中英

R - convert data.frame to multi-dimensional matrix

From example from a data.frame:

x = data.frame(c(1,1,2,2,3,3), c(1,2,1,2,1,2), c(1,1,1,2,2,2), c(12,14,22,24,34,28))
colnames(x)=c("Store","Dept","Year","Sales")

I would like to obtain:

Sales = array(NA, dim=c(2,2,2)) 

Sales being an array of 3 dimensions: (Store, Dept, Year) populated with all the data from x.

I am looking for a solution that scales to more dimensions, and thousands of records in the inital data frame (x).


Edit: I thought the solution below were working but it seems they are not exactly what I wanted. I think the problem is the indexing is lost in the process.

Here is a small data set:

    structure(list(Store = c(35L, 35L, 35L, 35L, 35L), Dept = c(71L, 
71L, 71L, 71L, 71L), Year = c(1, 2, 3, 4, 5), Sales = c(10908.04, 
12279.99, 11061.82, 12288.1, 9950.55)), .Names = c("Store", "Dept", 
"Year", "Sales"), row.names = c(NA, -5L), class = "data.frame")


    > x
  Store Dept Year    Sales
1    35   71    1 10908.04
2    35   71    2 12279.99
3    35   71    3 11061.82
4    35   71    4 12288.10
5    35   71    5  9950.55

Now I would like to be able to call Sales[35,71,2] to get 10908.04.

Both solutions below get the data by calling Sales[1,1,1], which is unusable for me at this point.

Something like :

tapply(X = x[["Sales"]], INDEX = x[setdiff(names(x), "Sales")], FUN = identity)

could work, but it is a bit strange to use tapply with the identity function.

Are you, perhaps, looking for xtabs ?

xtabs(Sales ~ Store + Dept + Year, x)
# , , Year = 1
# 
#      Dept
# Store  1  2
#     1 12 14
#     2 22  0
#     3  0  0
# 
# , , Year = 2
# 
#      Dept
# Store  1  2
#     1  0  0
#     2  0 24
#     3 34 28

You have to construct the array before with the appropriate dimension :

Sales <- array(NA, c(max(x$Store), max(x$Dept), max(x$Year)))

and then fill in the data :

for (i in 1:nrow(x)) 
    Sales[x[i,"Store"], x[i,"Dept"], x[i,"Year"]] <- x[i, "Sales"]

Sales[35,71,1]

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