I posted an answer to a question using dplyr and tidyr. Based on this comment I used Map to build the answer.
Next I tried to use base R tools only to answer the same question, but this didn't work as expected:
transform(
df,
Begin_New = Map(seq, Begin, End - 6000, list(by = 1000)) # or mapply(...)
)
caused an error:
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : Arguments imply different number of rows: 25, 33, 84, 36, 85, 165
Well, okay. That doesn't seem to work, but why does this one work?
df2 <- data.frame(id = 1:4, nested = c("a, b, f", "c, d", "e", "e, f"))
transform(df2, nested = strsplit(nested, ", "))
In my understanding Map(seq, Begin, End - 6000, list(by = 1000)) and strsplit(nested, ", ") both return a list() containing vectors. What am I missing?
I read this question Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : Arguments imply different number of rows: 1, 4, 5, 2 but still I don't know, why these two examples behave differently.
Data
df <- structure(list(ID = c("A01", "A01", "A01", "A01", "A01", "A01"
), Period = c("Baseline", "Run", "Recovery", "Baseline", "Run",
"Recovery"), Begin = c(0, 30500, 68500, 2000, 45000, 135000),
End = c(30500, 68500, 158000, 43000, 135000, 305000)), row.names = c(NA,
-6L), class = "data.frame")