I have data, which looks like this:

Data table

These data are only for one subject. I will have a lot more.
These data will be analyzed in R.

Now I'm storing them like this:

subject <- rep(1, times = 24)
measurement <- factor(x = rep(x = 1:3, each = 8), 
                      labels = c("Distance", "Frequency", "Energy"))
speed <- factor(x = rep(x = 1:2, each = 4, times = 3), 
                labels = c("speed1", "speed2"))
condition <- factor(x = rep(x = 1:2, each = 2, times = 6), 
                    labels = c("Control", "Experm"))
Try <- factor(x = rep(x = 1:2, times = 12), 
              labels = c("Try1", "Try2"))
result <- c(1:8, 

dt <- data.frame(subject, measurement, speed, condition, Try, result)

What is the appropriate way to store these data in R (in a data frame)?

  • $\begingroup$ This is a good way to show your results. But sometimes, if you want to calculate many things it's better to store the table in "long format". Google it, long and wide format. You can use cast, melt, reshape... $\endgroup$ – skan Jul 21 '15 at 15:55

Without more information all I can say is that:

  1. the say you're storing it is fine in general
  2. you can further transform/store your data depending on your use case

To expand on #2, if I want to study Distance vs Energy across all subjects, then I would format my data like this:

> library(reshape2)
> dt2 <- dt[dt$measurement %in% c('Distance','Energy'),]
> dt_cast <- dcast(dt2, subject+Try~measurement+speed+condition, value.var='result')

The transformed data (dt_cast) would then look like:

  subject  Try Distance_speed1_Control Distance_speed1_Experm Distance_speed2_Control
1       1 Try1                       1                      3                       5
2       1 Try2                       2                      4                       6
  Distance_speed2_Experm Energy_speed1_Control Energy_speed1_Experm Energy_speed2_Control
1                      7                    21                   23                    25
2                      8                    22                   24                    26
1                   27
2                   28

Allowing me to, for example, look at the relationship between the Distance_speed1_Control vs Energy_speed1_Control columns.

Basically subset/aggregate your data and then use the dcast to get the rows and columns the computer needs.

| improve this answer | |

This looks a well structured dataset. You can read more about database design in this section of wikipedia. Your data are well structured so querying is easy. As Jake C says, you'll want to transform it for specific tasks. Packages like dplr and reshape2 are excellent for this. You could also consider writing your data to a specific database. This is particularly useful if your dataset is so large that R runs out of RAM. I've written an example with SQLite here: https://scottishsnow.wordpress.com/2014/08/14/writing-to-a-database-r-and-sqlite/

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.