简体   繁体   中英

How can I split dataframe in r conditionally?

I want to split my dataframe (named as "data") into two groups (A and 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").

For group B, I want to assign the data that has the value of 0 in a specific column (same column, "x").

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

As Rui suggested, I have attached the result of 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

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

to get the separated data frames in A and B .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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