I am trying to use gather() and spread() on iris data set. I applied gather to get iris into new format:
Iris Original format:
Rows: 150
Columns: 5
$ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5....
$ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3....
$ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1....
$ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0....
$ Species <chr> "setosa", "setosa", "setosa", "setosa", "setosa", "s...
>
#Step 1
Mydata3<-gather(iris1, "measure", "value",1:2)
Resulting format:
Rows: 300
Columns: 5
$ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1....
$ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0....
$ Species <chr> "setosa", "setosa", "setosa", "setosa", "setosa", "s...
$ measure <chr> "Sepal.Length", "Sepal.Length", "Sepal.Length", "Sep...
$ value <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5....
>
#Step 2. Then I dropped first two to get next 3 columns
Mydata4 = Mydata3[ -c(1:2) ]
Resulting format:
Rows: 300
Columns: 3
$ Species <chr> "setosa", "setosa", "setosa", "setosa", "setosa", "setosa...
$ measure <chr> "Sepal.Length", "Sepal.Length", "Sepal.Length", "Sepal.Le...
$ value <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4....
>
Now I tried to reconstruct the original iris data format using spread()
Mydata5 = spread(Mydata3, measure, value)
Result:
Error: Each row of output must be identified by a unique combination of keys. Keys are shared for 144 rows: * 15, 36 * 3, 37, 39, 43 * 41, 42 * 13, 38 * 1, 2, 5, 9, 29, 34, 48, 50 * 7, 18, 46 * 10, 33 * 4, 8, 11, 28, 35, 40, 49 * 16, 22, 32 * 12, 26, 30, 31, 47 * 58, 94 * 61, 80 * 54, 72, 90 * 89, 100 * 95, 97 * 75, 98 * 66, 76 * 52, 67, 69, 79, 85 * 51, 64 * 127, 139 * 53, 73 * 124, 128 * 102, 143 * 117, 138 * 137, 141 * 165, 186 * 153, 187, 189, 193 * 191, 192 * 163, 188 * 151, 152, 155, 159, 179, 184, 198, 200 * 157, 168, 196 * 160, 183 * 154, 158, 161, 178, 185, 190, 199 * 166, 172, 182 * 162, 176, 180, 181, 197 * 208, 244 * 211, 230 * 204, 222, 240 * 239, 250 * 245, 247 * 225, 248 * 216, 226 * 202, 217, 219, 229, 235 * 201, 214 * 277, 289 * 203, 223 * 274, 278 * 252, 293 * 267, 288 * 287, 291
My goal is to construct something like this
$ Species <chr> "setosa", "setosa", "setosa", "setosa", "setosa", "s...
$ n <int> 1, 2, 3, 4, 5 ,6 ,7 ,8 ,9 ,10, 11, 12, 13, 14, 15, 16..
$ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1....
$ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0....
$ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5....
$ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3....
Can someone help me understand why spread would not work here? I would still want to use spread function for it.
Thanks.