简体   繁体   中英

Does dplyr or plyr have a remainder function?

Does dplyr or plyr have a remainder function?

Let say I have a series of MODE (1-25) to look into

for example dataset Main

idc3 = c("23|24")

column0|column1|column2|column3|MODE1|MODE2|MODE3
     4 |  83   |   23  |   863 | 85  | 86  |  45
    53 |  26   |   9   |   153 | 23  | 34  |  85
    33 |  66   |   91  |   693 | 95  | 23  |  74
     6 |  87   |   27  |   863 | 47  | 56  |  52
    57 |  27   |   9   |   153 | 78  | 38  |  64
    37 |  67   |   97  |   693 | 34  | 86  |  24

cut  <- Main[unique(grep(paste(idc3), Main$MODE1)), ]
cut2 <- Main[unique(grep(paste(idc3), Main$MODE2)), ]
cut3 <- Main[unique(grep(paste(idc3), Main$MODE3)), ]

  column0|column1|column2|column3|MODE1|MODE2|MODE3
      53 |  26   |   9   |  153  | 23  | 34  |  85
      33 |  66   |   91  |  693  | 95  | 23  |  74
      37 |  67   |   97  |  693  | 34  | 86  |  24

What if I would like the remainder dataset

column0|column1|column2|column3|MODE|MODE2|MODE3
     4 |  83   |   23  |   863 | 85 | 86  |  45        
     6 |  87   |   27  |   863 | 47 | 56  |  52
    57 |  27   |   9   |   153 | 78 | 38  |  64

Is there a way to do this? Thanks

Instead of creating three separate cuts, you can create an index with one function call. You will then be able to choose the matched columns or opposite as needed:

idc3 = c(23,24)
indx <- as.logical(rowSums(sapply(df[,5:7], `%in%`, idc3)))
df[indx,]
#   column0 column1 column2 column3 MODE1 MODE2 MODE3
# 2      53      26       9     153    23    34    85
# 3      33      66      91     693    95    23    74
# 6      37      67      97     693    34    86    24
df[!indx,]
#   column0 column1 column2 column3 MODE1 MODE2 MODE3
# 1       4      83      23     863    85    86    45
# 4       6      87      27     863    47    56    52
# 5      57      27       9     153    78    38    64

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