如何有条件地在r中拆分数据帧?

时间:2019-05-02 17:16:41

标签: r

我想将数据框(命名为“数据”)分成两组(A和B)。

对于组A,我想在特定列中分配值为1的数据(假设列名称为“ x”)。

对于B组,我想在特定列(同一列“ x”)中分配值为0的数据。

我对拆分功能进行了一些研究,但是找不到与我的案例有关的任何信息。

如果我的问题太含糊,请发表评论并让我知道,而不是结束此问题。我将附上一些代码以使其清楚。

谢谢!

编辑1

正如Rui所建议的,我已经附上了dput的结果。但是,由于我的数据很大,所以我做了

dput(head(dataSetTrim, 10)) instead of dput(head(dataSetTrim, 20))
> dput(head(dataSetTrim, 10))
structure(list(sp16ap = c("Yes", "No", "Yes", "Yes", "Yes", "Yes", 
"No", "Yes", "Yes", "No"), sp17abscore = c("3", NA, NA, "4", 
"Exam not taken", "Exam not taken", NA, "3", "3", NA), sp17abyear = c(12, 
NA, NA, 12, 12, 12, NA, NA, 12, NA), sp17abgrade = c(3, NA, NA, 
3.67, 4, 2.67, NA, NA, 4, NA), sp17bcscore = c(NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_), 
    sp17bcyear = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), sp17bcgrade = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), sp17statscore = c(NA, NA, 
    "4", NA, NA, NA, NA, NA, NA, NA), sp17statyear = c(NA, NA, 
    12, NA, NA, NA, NA, NA, NA, NA), sp17statgrade = c(NA, NA, 
    4, NA, NA, NA, NA, NA, NA, NA), Q3FUS_Yes = c("Yes", " ", 
    " ", " ", " ", " ", " ", " ", " ", "Yes"), Q3FUS_No = c(" ", 
    " ", " ", " ", "No", " ", "No", " ", " ", " "), switchPersist = c(12, 
    16, 21, 16, 2, 22, 2, 21, 16, 12), SWP = c(0, 0, 0, 0, 1, 
    0, 1, 0, 0, 0)), row.names = c(1L, 2L, 3L, 4L, 5L, 7L, 8L, 
9L, 10L, 11L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

您可以仅使用常规命令选择行。如果要根据列SWP的值进行拆分,可以编写

dataSetTrim <- ...your data...
A <- dataSetTrim[dataSetTrim$SWP==1,]
B <- dataSetTrim[dataSetTrim$SWP==0,]

获取AB中分离的数据帧。