[R] get is.na & !is.na count of various combinations of columns
arun
smartpink111 at yahoo.com
Wed Mar 19 05:59:30 CET 2014
If you want to identify the combination of columns:
names1 <- unlist(lapply(seq(length(vec1)-1),function(i) {x1 <- as.data.frame(combn(vec1,i),stringsAsFactors=FALSE); unlist(lapply(x1, function(x) {indx <- vec1 %in% x; paste(paste(paste0("!is.na","(", vec1[!indx],")"),collapse=" & "), paste(paste0("is.na","(", vec1[indx],")"),collapse= " & "), sep= " & ") }))}),use.names=FALSE)
res1 <- unlist(res,recursive=FALSE)
names(res1) <- names1
res1
length(res1)
#[1] 30
res1[30]
#$`!is.na(Poster) & is.na(Records) & is.na(MWBW) & is.na(OT) & is.na(CL)`
# CaseID Structure Poster Records MWBW OT CL
#6 1605951 prop 1 NA NA NA NA
res2 <- res1[sapply(res1,nrow)!=0]
res2[4]
#$`!is.na(Poster) & !is.na(Records) & is.na(MWBW) & is.na(OT) & is.na(CL)`
# CaseID Structure Poster Records MWBW OT CL
#1 1605928 corp 1 1 NA NA NA
#2 1605943 corp 1 1 NA NA NA
Hope this helps.
A.K.
On , arun <smartpink111 at yahoo.com> wrote:
Hi,
May be this helps:
res <- lapply(seq(length(vec1)-1),function(i) {x1 <- as.data.frame(combn(vec1,i),stringsAsFactors=FALSE); lapply(x1, function(x) {indx <- vec1 %in% x; nisna <- paste(paste0("!is.na","(", vec1[!indx],")"),collapse=" & ");isna <- paste(paste0("is.na","(", vec1[indx],")"),collapse= " & ");subset(mydatatest, eval(parse(text=nisna)) & eval(parse(text=isna)))})})
A.K.
On Tuesday, March 18, 2014 11:45 PM, bcrombie <bcrombie at utk.edu> wrote:
I'm trying to count the number of combinations of columns containing data &
not containing data as described below. I"m not sure how to do this in R
and need some help.
#get TRUE/FALSE count of various combinations of columns per CaseID or per
Structure
mydatatest <- data.frame (CaseID = c("1605928", "1605943", "1605945",
"1605947", "1605949", "1605951"),
Structure = c("corp", "corp", "prop", "LLC", "LLC",
"prop"),
Poster = c(1, 1, 1, NA, NA, 1),
Records = c(1, 1, 1, 1, 1, NA),
MWBW = c(NA, NA, 495.10, NA, NA, NA),
OT = c(NA, NA, NA, NA, 411.25, NA),
CL = c(NA, NA, NA, 13.52, NA, NA))
combo1 <- subset(mydatatest, !is.na(Poster & Records & MWBW & OT & CL))
combo2 <- subset(mydatatest, !is.na(Poster & Records & MWBW & OT) &
is.na(CL))
combo3 <- subset(mydatatest, !is.na(Poster & Records & MWBW) & is.na(OT &
CL))
combo4 <- subset(mydatatest, !is.na(Poster & Records) & is.na(MWBW & OT &
CL))
combo5 <- subset(mydatatest, !is.na(Poster) & is.na(Records & MWBW & OT &
CL))
combo6 <- subset(mydatatest, !is.na(Records) & is.na(Poster & MWBW & OT &
CL))
combo7 <- subset(mydatatest, !is.na(MWBW) & is.na(Poster & Records & OT &
CL))
combo8 <- subset(mydatatest, !is.na(OT) & is.na(Poster & Records & MWBW &
CL))
combo9 <- subset(mydatatest, !is.na(CL) & is.na(Poster & Records & MWBW &
OT))
etc.
--
View this message in context: http://r.789695.n4.nabble.com/get-is-na-is-na-count-of-various-combinations-of-columns-tp4687084.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list