1

Let's assume I have a dataframe in the following format, obtained from a .csv file:

Measurement    Config  Value
---------------------------    _
Time           A       10       |      
Object         A       20       | Run 1    
Nodes          A       30      _|     
Time           A       8        |     
Object         A       18       | Run 2
Nodes          A       29      _|
Time           B       9        |
Object         B       20       | Run 3
Nodes          B       35      _|
...

There are a fixed number of Measurements that are taken during each run, and each run is run with a given Config. The Measurements per run are fixed (e.g., every run consists of a Time, an Objects and a Nodes measurement in the example above), but there can be multiple runs for a single config (e.g., Config A was run two times in the example above, B only once)

My primary goal is to plot correlations (scatter plots) between two of those measurement types, e.g., plot Objects (x-axis) against Nodes (y-axis) and highlight different Configs (color)

I thought that this could be best achieved if the dataframe is in the following format:

Config  Time  Objects  Nodes
--------------------------
A       10    20       30         <- Run 1
A       8     18       29         <- Run 2
B       9     20       35         <- Run 3

I.e., creating the columns based on the factor-values of the Measurement-column, and assigning the respective Value-value to the cells.

Is there an "easy" way in R to achieve that?

Jaap
  • 81,064
  • 34
  • 182
  • 193
Markus Weninger
  • 11,931
  • 7
  • 64
  • 137

1 Answers1

2

First create a run variable:

# option 1:
d$run <- ceiling(seq_along(d$Measurement)/3)

# option 2:
d$run <- 1 + (seq_along(d$Config)-1) %/% 3

Then you reshape to wide wide format with the dcast function from reshape2 or data.table:

reshape2::dcast(d, Config + run ~ Measurement, value.var = 'Value')

you will then get:

  Config run Nodes Object Time
1      A   1    30     20   10
2      A   2    29     18    8
3      B   3    35     20    9
Jaap
  • 81,064
  • 34
  • 182
  • 193