Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reshaping an array to data.frame

I have the following data structure (an "atomic vector?") output from daply in plyr, in which I had the function return three different measures for each subject, condition, and item.

x = structure(c(-0.93, 0.39, 0.88, 0.63, 0.86, -0.69, 1.02, 0.29, 0.94,  0.93, -0.01, 0.79, 0.32, 0.14, 0.13, -0.07, -0.63, 0.26, 0.07, 0.87, -0.36, 1.043, 0.33, -0.12, -0.055, 0.07, 0.67, 0.48, 0.002, 0.008,  -0.19, -1.39, 0.98, 0.43, -0.02, -0.15,-0.08, 0.74, 0.96, 0.44, -0.005, 1.09, 0.36, 0.04, 0.09, 0.17, 0.68, 0.51, 0.09, 0.12, -0.05, 0.11, 0.99, 0.62, 0.13, 0.06, 0.27, 0.74, 0.96, 0.45), .Dim = c(5L,  2L, 2L, 3L), .Dimnames = structure(list(Subject = c("s1", "s2",  "s3", "s4", "s5"), Cond = c("A", "B"), Item = c("1", "2"), c("Measure1",  "Measure2", "Measure3")), .Names = c("Subject", "Cond",  "Item", ""))) 

I want to change it to look like:

Subject Cond Item Measure1 Measure2 Measure3      s1    A    1    -0.93   -0.360   -0.005      s1    A    2    -0.01    -0.19    -0.05       s1    B    1    -0.69    0.070     0.17      s1    B    2    -0.07    -0.15     0.06      s2    A    1     0.39    1.043    1.090      s2    A    2     0.79    -1.39     0.11      s2    B    1     1.02    0.670     0.68      s2    B    2    -0.63    -0.08     0.27 

etc.

Is there an easy way to do this?

like image 331
Amyunimus Avatar asked Jun 21 '12 15:06

Amyunimus


People also ask

How do I turn an array into a data frame?

How do you convert an array to a DataFrame in Python? To convert an array to a dataframe with Python you need to 1) have your NumPy array (e.g., np_array), and 2) use the pd. DataFrame() constructor like this: df = pd. DataFrame(np_array, columns=['Column1', 'Column2']) .

How do you reshape an array of data?

reshape() function allows us to reshape an array in Python. Reshaping basically means, changing the shape of an array. And the shape of an array is determined by the number of elements in each dimension. Reshaping allows us to add or remove dimensions in an array.

What does reshaping an array do?

Reshaping arrays Reshaping means changing the shape of an array. The shape of an array is the number of elements in each dimension. By reshaping we can add or remove dimensions or change number of elements in each dimension.


1 Answers

Use as.data.frame.table().

d0 <- as.data.frame.table(x) head(d0)  #   Subject Cond Item     Var4  Freq # 1      s1    A    1 Measure1 -0.93 # 2      s2    A    1 Measure1  0.39 # 3      s3    A    1 Measure1  0.88 # 4      s4    A    1 Measure1  0.63 # 5      s5    A    1 Measure1  0.86 # 6      s1    B    1 Measure1 -0.69  library(tidyr) d1 <- pivot_wider(data = d0, names_from = "Var4", values_from = "Freq") head(d1)  #   Subject Cond Item Measure1 Measure2 Measure3 # 1      s1    A    1    -0.93   -0.360   -0.005 # 2      s1    A    2    -0.01   -0.190   -0.050 # 3      s1    B    1    -0.69    0.070    0.170 # 4      s1    B    2    -0.07   -0.150    0.060 # 5      s2    A    1     0.39    1.043    1.090 # 6      s2    A    2     0.79   -1.390    0.110 
like image 105
guyabel Avatar answered Sep 30 '22 13:09

guyabel