bcrombie
2014-Mar-19 02:48 UTC
[R] get is.na & !is.na count of various combinations of columns
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.
arun
2014-Mar-19 04:47 UTC
[R] get is.na & !is.na count of various combinations of columns
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.