Note that there are some explanatory texts on larger screens.

plurals
  1. PORe-align column in data frame into multiple columns
    text
    copied!<p>I'm trying to change a data frame column (<code>var3</code>, in the example below) that has multiple values for factor levels of another variable (<code>names</code>, in the example below). I'd like <code>var3</code> to be split into separate columns, one for each value, so that the factor levels in <code>names</code> do not repeat. My other variables (<code>var1</code>, <code>var2</code>) repeat where necessary to provide space for <code>var3</code>.</p> <p>This is the kind of data I have:</p> <pre><code>df1 &lt;- structure(list(name = structure(c(2L, 4L, 4L, 4L, 3L, 5L, 5L, 1L), .Label = c("fifth", "first", "fourth", "second", "third" ), class = "factor"), var1 = c(90L, 84L, 84L, 84L, 18L, 22L, 22L, 36L), var2 = c(301L, 336L, 336L, 336L, 412L, 296L, 296L, 357L), var3 = c(-0.582075925, -1.108889624, -1.014962009, -0.162309524, -0.282309524, 0.563055819, -0.232075925, -0.773353424)), .Names = c("name", "var1", "var2", "var3"), class = "data.frame", row.names = c(NA, -8L)) </code></pre> <p>This is what i'd like:</p> <pre><code>df2 &lt;- structure(list(name = structure(c(2L, 4L, 3L, 5L, 1L), .Label = c("fifth", "first", "fourth", "second", "third"), class = "factor"), var1 = c(90L, 84L, 18L, 22L, 36L), var2 = c(301L, 336L, 412L, 296L, 357L), var3 = c(-0.582075925, -1.108889624, -0.282309524, 0.563055819, -0.773353424), var3.2 = c(NA, -1.014962009, NA, -0.232075925, NA), var3.3 = c(NA, -0.162309524, NA, NA, NA)), .Names = c("name", "var1", "var2", "var3", "var3.2", "var3.3"), class = "data.frame", row.names = c(NA, -5L)) </code></pre> <p>I've looked at <code>reshape</code> and <code>ddply</code>, but can't get them to give me this output.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload