我的数据帧只包含是/否字符串和NA。类似的东西:
X1 X2 X3 Y1 Y2 Y3 Z1 Z2 Z3
1 <NA> no yes <NA> yes no <NA> <NA> <NA>
2 <NA> yes no yes <NA> yes yes <NA> yes
3 yes yes <NA> yes yes yes <NA> <NA> yes
4 yes <NA> no <NA> yes no <NA> <NA> <NA>
5 <NA> no yes <NA> yes no yes no <NA>
6 <NA> <NA> no <NA> yes no <NA> <NA> <NA>
7 no no yes no no yes yes no <NA>
8 yes yes no no no yes no no no
9 yes <NA> yes <NA> yes no yes yes yes
10 no no <NA> yes no no yes yes no
我想知道如何知道有多少行包括&#34;是&#34;在任何&#39; 3&#39; var(X3 / Y3 / Z3&#39;和a&#34; no&#34; / na在ALL&#39; 1/2&#39; vars(X1 / X2 / Y1 / Y2等)中的df
使用以下内容创建的样本集:
Data <- data.frame(
X1 = sample(c("yes", "no", NA), 10, replace = TRUE),
X2 = sample(c("yes", "no", NA), 10, replace = TRUE),
X3 = sample(c("yes", "no", NA), 10, replace = TRUE),
Y1 = sample(c("yes", "no", NA), 10, replace = TRUE),
Y2 = sample(c("yes", "no", NA), 10, replace = TRUE),
Y3 = sample(c("yes", "no", NA), 10, replace = TRUE),
Z1 = sample(c("yes", "no", NA), 10, replace = TRUE),
Z2 = sample(c("yes", "no", NA), 10, replace = TRUE),
Z3 = sample(c("yes", "no", NA), 10, replace = TRUE)
)
答案 0 :(得分:0)
只要您按照现在的方式组织数据帧,即comp1type1,comp1type2,comp1type3,comp2type1,...,comp [I] type [J]。我相信你可以使用以下方法。
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" | is.na(vecone)) == ncol(vecone)
condition2 <- rowSums(vectwo=="No" | is.na(vectwo)) == ncol(vectwo)
# this third condition will find which rows type3 have atleast one Yes
condition3 <- rowSums(vecthree =="Yes",na.rm=T) >= 1
现在您可以使用这些条件来确定任何类型3包含“是”的行数和所有类型1/2的“否/ NA”
sum(condition1 & condition2 & condition3)