简体   繁体   English

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

[英]How can I split dataframe in r conditionally?

I want to split my dataframe (named as "data") into two groups (A and B). 我想将数据框(命名为“数据”)分为两组(A和B)。

For group A, I want to assign the data that has the value of 1 in a specific column (suppose column name is "x"). 对于组A,我想在特定列中分配值为1的数据(假设列名称为“ x”)。

For group B, I want to assign the data that has the value of 0 in a specific column (same column, "x"). 对于B组,我想在特定列(同一列“ x”)中分配值为0的数据。

I did some research about split function but I was unable to find any relevant source to my case. 我对拆分功能进行了一些研究,但是找不到与我的案例有关的任何信息。

If my question is too vague, please comment it and let me know instead of closing this question. 如果我的问题太含糊,请发表评论并让我知道,而不是结束此问题。 I will attach some of my codes to make it clear. 我将附上一些代码以使其清楚。

Thank you! 谢谢!

EDIT 1 编辑1

As Rui suggested, I have attached the result of dput. 正如Rui所建议的,我已经附加了dput的结果。 However, since my data is pretty big, I did 但是,由于我的数据很大,所以我做了

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")

You can just use the usual commands to select rows. 您可以只使用通常的命令来选择行。 If you want to split according to the value of the column SWP , you can write 如果要根据列SWP的值进行拆分,可以编写

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

to get the separated data frames in A and B . 以获得AB分离的数据帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM