【发布时间】:2021-10-30 22:39:19
【问题描述】:
我使用dplyr 和tidyr 向问题发布了answer。基于this comment 我使用Map 来构建答案。
接下来我尝试使用base R 工具仅回答相同的问题,但这并没有按预期工作:
transform(
df,
Begin_New = Map(seq, Begin, End - 6000, list(by = 1000)) # or mapply(...)
)
导致错误:
错误(函数(...,row.names = NULL,check.rows = FALSE,check.names = TRUE,:参数暗示不同的行数:25、33、84、36、85、165
好吧,好吧。这似乎不起作用,但是为什么这个起作用?
df2 <- data.frame(id = 1:4, nested = c("a, b, f", "c, d", "e", "e, f"))
transform(df2, nested = strsplit(nested, ", "))
据我了解,Map(seq, Begin, End - 6000, list(by = 1000)) 和 strsplit(nested, ", ") 都返回包含向量的 list()。我错过了什么?
我阅读了这个问题Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : Arguments imply different number of rows: 1, 4, 5, 2,但我仍然不知道,为什么这两个示例的行为不同。
数据
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")
【问题讨论】:
-
仅供参考,
within(df, { Begin_New = Map(...); })有效。不知道为什么。
标签: r list dataframe dictionary