gpt4 book ai didi

r - 拆分和删除分组变量

转载 作者:行者123 更新时间:2023-12-04 10:26:26 25 4
gpt4 key购买 nike

我正在尝试将数据框拆分为列表。 This question很有帮助,但我也想删除用于分组的列,因为它会弄乱以后的步骤。 drop split 的论据仅适用于未使用的级别。数据框如下:

structure(list(Var1 = c(-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L, -1L, -1L,
-1L, -1L, -1L, -1L, -1L),
Var2 = c(-1L, -1L, -1L, -1L, 0L, -1L, -1L, -1L, 0L,
0L, 0L, -1L, -1L, -1L, -1L, -1L, -1L, -1L,
-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L,
-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L),
Var3 = c(1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, -1L, -1L, -1L, -1L,
-1L, -1L, -1L, -1L),
Var4 = c(1L, -1L, -1L, 2L, -1L, -1L, -1L, 1L, 1L, 1L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, -1L, -1L, -1L, -1L,
-1L, -1L, -1L, -1L),
Var5 = c(1L, -1L, -1L, 2L, -1L, -1L, -1L, 2L, 1L, 1L,
1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L,
0L, 0L, 1L, 1L, 1L, 1L, -1L, -1L, -1L, -1L,
-1L, -1L, -1L, -1L),
Bin = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 6L, 6L,
7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L,
12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L,
16L, 17L, 17L, 18L, 18L)),
.Names = c("Var1", "Var2", "Var3", "Var4", "Var5", "Bin"),
class = "data.frame", row.names = c(NA, -35L))

如何在删除“Bin”的同时通过“Bin”拆分它?

最佳答案

根据您关于此列的信息,您可以使用

split(df[, -ncol(df)], df$Bin)

如果你知道这是最后一个,并且
split(df[, !names(df) == "Bin"], df$Bin)

如果你只知道它的名字。还
split(df[, -which(names(df) == "Bin")], df$Bin)


split(df[, -match("Bin", names(df))], df$Bin)

关于r - 拆分和删除分组变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49495912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com