dA 有这样的数据表
id group startPoints endPoints
1 A 4, 20, 50, 63, 8, 25, 60, 78
1 A 120, 300, 231, 332
1 B 500, 550
1 B 650, 800 700, 820
1 C 830, 900, 950 850, 920, 970
我想要实现的是EndPoint - StartPoint
在特定组中获得长度()的 SUM/MEAN/等,但不能与 sapply 一起使用
我的目标是得到表格的结果:
Group SUM
A 177
B 120
C 60
我正在尝试将两件事结合起来
lengths <- strsplit(as.character(table$endPoints), ",", fixed=TRUE)
和
y <- factor(table$group)
tapply(lengths, y, sum)
但我被困住了,无法让它工作。
添加样本数据
structure(list(id = c(1L, 1L, 1L, 1L, 1L), group = structure(c(1L,
1L, 2L, 2L, 3L), .Label = c("A", "B", "C"), class = "factor"),
startPoints = structure(c(2L, 1L, 3L, 4L, 5L), .Label = c("120,300,",
"4,20,50,63,", "500,", "650,800,", "830,900,950,"), class = "factor"),
endPoints = structure(c(4L, 1L, 2L, 3L, 5L), .Label = c("231,332,",
"550,", "700,820,", "8,25,60,78", "850,920,970,"), class = "factor")),
.Names = c("id", "group", "startPoints", "endPoints"), class = "data.frame",
row.names = c(NA, -5L))