一种方法是在数据框中找到列的名称并以此为基础进行拆分。我们可以group_split
用来将数据拆分为数据框,我们用来map_at
从每个列表中删除第一行,因为它是列名,并type.convert
用来将列转换为各自的类。
library(dplyr)
library(purrr)
temp <- df %>%
group_split(id = cumsum(A == names(.)[1]) + 1) %>%
map_at(-1, tail, -1) %>%
map(type.convert)
temp
#[[1]]
# A tibble: 3 x 5
# A B C D id
# <dbl> <dbl> <dbl> <dbl> <int>
#1 0.668 0.411 0.553 0.477 1
#2 0.794 0.821 0.530 0.732 1
#3 0.108 0.647 0.789 0.693 1
#[[2]]
# A tibble: 6 x 5
# A B C D id
# <dbl> <dbl> <dbl> <dbl> <int>
#1 0.724 0.783 0.023 0.478 2
#2 0.861 0.099 0.407 0.332 2
#3 0.438 0.316 0.913 0.651 2
#4 0.245 0.519 0.294 0.258 2
#5 0.07 0.662 0.459 0.479 2
#6 0.766 0.839 0.892 0.961 2
#[[3]]
# A tibble: 4 x 5
# A B C D id
# <dbl> <dbl> <dbl> <dbl> <int>
#1 0.084 0.347 0.864 0.435 3
#2 0.875 0.334 0.39 0.713 3
#3 0.339 0.476 0.777 0.4 3
#4 0.084 0.347 0.864 0.435 3
在基础 R 中使用相同的逻辑,我们可以做
df$id <- cumsum(df$A == names(df)[1]) + 1
temp <- split(df, df$id)
temp[-1] <- lapply(temp[-1], tail, -1)
temp <- lapply(temp, type.convert)
如果您需要它们作为单独的数据框,
names(temp) <- paste0("df", seq_along(temp))
list2env(temp, .GlobalEnv)
数据
df <- structure(list(A = structure(c(7L, 10L, 3L, 13L, 8L, 11L, 6L,
4L, 1L, 9L, 13L, 2L, 12L, 5L, 2L), .Label = c("0.070", "0.084",
"0.108", "0.245", "0.339", "0.438", "0.668", "0.724", "0.766",
"0.794", "0.861", "0.875", "A"), class = "factor"), B = structure(c(5L,
11L, 8L, 13L, 10L, 1L, 2L, 7L, 9L, 12L, 13L, 4L, 3L, 6L, 4L), .Label = c("0.099",
"0.316", "0.334", "0.347", "0.411", "0.476", "0.519", "0.647",
"0.662", "0.783", "0.821", "0.839", "B"), class = "factor"),
C = structure(c(7L, 6L, 9L, 13L, 1L, 4L, 12L, 2L, 5L, 11L,
13L, 10L, 3L, 8L, 10L), .Label = c("0.023", "0.294", "0.390",
"0.407", "0.459", "0.530", "0.553", "0.777", "0.789", "0.864",
"0.892", "0.913", "C"), class = "factor"), D = structure(c(5L,
11L, 9L, 13L, 6L, 2L, 8L, 1L, 7L, 12L, 13L, 4L, 10L, 3L,
4L), .Label = c("0.258", "0.332", "0.400", "0.435", "0.477",
"0.478", "0.479", "0.651", "0.693", "0.713", "0.732", "0.961",
"D"), class = "factor")), class = "data.frame", row.names = c(NA, -15L))