实际上我有一个大表,我想根据特定列的值将其拆分为多个表。 为此,我使用这样的子集函数:
lapply(1:18, function(i) subset(table,bucket==i))
问题是我不知道如何为每个迷你表分配其名称,例如 table_1 for i=1, table_2 for i=2 ...
我这样做了并且有效:
for (i in unique(table$bucket))
{
assign( paste("table_", i, sep = ""), subset(table,bucket==i) )
}
你知道是否有办法避免使用 For 循环?
这个怎么样?
install.packages(dplyr)
library(dplyr)
sampler1 <- data.frame(LobsterID=c("Lobster1","Lobster1","Lobster2",
"Lobster2","Lobster2","Lobster2",
"Lobster2","Lobster3","Lobster3","Lobster3"),
Area=c(.4,.35,1.1,1.04,1.14,1.1,1.05,1.7,1.63,1.8),
Sampler=c(rep("Sampler1", 10)))
sampler2 <- data.frame(LobsterID=c("Lobster1","Lobster1","Lobster1",
"Lobster1","Lobster1","Lobster2",
"Lobster2","Lobster2","Lobster4","Lobster4"),
Area=c(.41,.44,.47,.43,.38,1.14,1.11,1.09,1.41,1.4),
Sampler=c(rep("Sampler2", 10)))
combined <- bind_rows(sampler1, sampler2)
combined
splitout <- split(combined, combined$LobsterID)
splitout
结果:
$Lobster1
LobsterID Area Sampler
1 Lobster1 0.40 Sampler1
2 Lobster1 0.35 Sampler1
11 Lobster1 0.41 Sampler2
12 Lobster1 0.44 Sampler2
13 Lobster1 0.47 Sampler2
14 Lobster1 0.43 Sampler2
15 Lobster1 0.38 Sampler2
$Lobster2
LobsterID Area Sampler
3 Lobster2 1.10 Sampler1
4 Lobster2 1.04 Sampler1
5 Lobster2 1.14 Sampler1
6 Lobster2 1.10 Sampler1
7 Lobster2 1.05 Sampler1
16 Lobster2 1.14 Sampler2
17 Lobster2 1.11 Sampler2
18 Lobster2 1.09 Sampler2
$Lobster3
LobsterID Area Sampler
8 Lobster3 1.70 Sampler1
9 Lobster3 1.63 Sampler1
10 Lobster3 1.80 Sampler1
$Lobster4
LobsterID Area Sampler
19 Lobster4 1.41 Sampler2
20 Lobster4 1.40 Sampler2