How do I pick the rows in data frame with at least one variable with non-missing value?

sunflower :

In a dataframe, I only want to keep rows that have at least one variables starting with DSDECOD is NOT empty. How can I do that?

it seems that following code works.

ds_sub <- subset(ds_supp, (DSDECOD1 !="" | DSDECOD2 !="" |
    DSDECOD3 !="" | DSDECOD4 !=""))

But is there simple way so that I don't have to write out all of the variables starting with DSDECOD?

Edward :

Maybe using rowSums and grepl:

ds_supp[rowSums(ds_supp[, grepl("^DSDECOD", names(ds_supp))]!="")>0,]

  ID DSDECOD1 DSDECOD2 DSDECOD3 DSDECOD4
1  1                          B         
2  2        A                 A        A
3  3        B                          B
5  5        C                 C        C
6  6                          D        D

Data:

  ID DSDECOD1 DSDECOD2 DSDECOD3 DSDECOD4
1  1                          B         
2  2        A                 A        A
3  3        B                          B
4  4                                     # <- empty row
5  5        C                 C        C
6  6                          D        D

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=375764&siteId=1
Recommended