Selecting rows in R with “Yes” in one column of a set of columns, and NOT “Yes” in all columns of another set of columns

as long as you have the dataframe organized the way you do now i.e., comp1type1,comp1type2,comp1type3,comp2type1,…,comp[I]type[J]. I am sure you can use the following method.

    ncomp <- 20
    ntype <- 3
    vecone <- df[,seq(1,ncomp*ntype,ntype)]
    vectwo <- df[,seq(2,ncomp*ntype,ntype)]
    vecthree <- df[,seq(3,ncomp*ntype,ntype)]

    # now that we have the vectors of types seperated into data.frame's
    # it'll be easier to do what we want
    # this first condition will find which rows type1/2 have No or NA for all
    condition1 <- rowSums(vecone=="No" | == ncol(vecone)
    condition2 <- rowSums(vectwo=="No" | == ncol(vectwo)
    # this third condition will find which rows type3 have atleast one Yes
    condition3 <- rowSums(vecthree =="Yes",na.rm=T) >= 1

Now you may use these conditions to figure out how many rows include a Yes for any type3 and a No/NA for all type1/2

sum(condition1 & condition2 & condition3)

Browse More Popular Posts

Leave a Comment