I have subsetted the data so it is easier to demonstrate what I am attempting to do. I am trying to create a data frame with a new row for the value in the column "MaxRounds". At first MaxRounds was in a column like so:
library(dplyr);library(tidyr);library(splitstackshape)
structure(list(power = c(0.800962297001584, 0.804719517260326,
0.808410477932415, 0.812036218849852, 0.803164810470566, 0.815597767274311
), nights = c(20L, 20L, 20L, 20L, 19L, 20L), sites = c(78L, 79L,
80L, 81L, 81L, 82L), NonRoundedMaxRounds = c(3, 3, 3, 3, 3.15789473684211,
3), MaxRounds = c(3, 3, 3, 3, 3, 3)), row.names = c(NA, 6L), class = "data.frame")
I then created new rows that are dependent on the MaxRounds column = creating duplicate rows dependent on the number of MaxRounds. For example, if the MaxRounds are 2, then 1-2 rows are created, if the MaxRounds are 5 then 5 rows are created).
The code creates a unique ID row name: x, x.1, x.2, x.3 etc.
data = expandRows(data, "MaxRounds")
structure(list(power = c(0.800962297001584, 0.800962297001584,
0.800962297001584, 0.804719517260326, 0.804719517260326, 0.804719517260326
), nights = c(20L, 20L, 20L, 20L, 20L, 20L), sites = c(78L, 78L,
78L, 79L, 79L, 79L), NonRoundedMaxRounds = c(3, 3, 3, 3, 3, 3
)), row.names = c("1", "1.1", "1.2", "2", "2.1", "2.2"), class = "data.frame")
I then created a new column based on the row names:
data$RowID = rownames(data)
structure(list(power = c(0.800962297001584, 0.800962297001584,
0.800962297001584, 0.804719517260326, 0.804719517260326, 0.804719517260326
), nights = c(20L, 20L, 20L, 20L, 20L, 20L), sites = c(78L, 78L,
78L, 79L, 79L, 79L), NonRoundedMaxRounds = c(3, 3, 3, 3, 3, 3
), RowID = c("1", "1.1", "1.2", "2", "2.1", "2.2")), row.names = c("1",
"1.1", "1.2", "2", "2.1", "2.2"), class = "data.frame")
Next I am attempting to group together all of the rows that have the same x value (despite the decimal point) and number them sequentially. For example:
- 1, 1.1, 1.2 = 1, 2, 3
- 2, 2.1, 2.1 = 1, 2, 3
I am attempting to group by the column "RowID" using:
data %>% group_by(RowID) %>% mutate(id = row_number())
But I get this error: