Suppose i have matrix z id<-c(1,2,3,4,5) a <- c(4, 3, 2, NA, 1) b <- c(3, NA, 2, 7, 1) c <-c(3, NA, NA, 7, NA) z<- cbind(id,a, b,c) id a b c [1,] 1 4 3 3 [2,] 2 3 NA NA [3,] 3 2 2 NA [4,] 4 NA 7 7 [5,] 5 1 1 NA I want to select those columns for which I have the smallest number of NA In my example, these would be column1 and 2 since they give me 4 3 2 2 1 1 My real example have alot of columns and I want to find and save all possible subsets Thanks, alot Evgenia -- View this message in context: http://r.789695.n4.nabble.com/Select-maximum-subset-tp4653845.html Sent from the R help mailing list archive at Nabble.com.
Hi, try this: ?z1<-data.frame(z) z1[colSums(is.na(z1))<=1] #? id? a? b #1? 1? 4? 3 #2? 2? 3 NA #3? 3? 2? 2 #4? 4 NA? 7 #5? 5? 1? 1 A.K. ----- Original Message ----- From: Evgenia <evgts at aueb.gr> To: r-help at r-project.org Cc: Sent: Sunday, December 23, 2012 4:16 PM Subject: [R] Select maximum subset Suppose i have matrix z id<-c(1,2,3,4,5) a <- c(4, 3, 2, NA, 1) b <- c(3, NA, 2, 7, 1) c <-c(3, NA, NA, 7, NA) z<- cbind(id,a, b,c) ? ? id? a? b? c [1,]? 1? 4? 3? 3 [2,]? 2? 3 NA NA [3,]? 3? 2? 2 NA [4,]? 4 NA? 7? 7 [5,]? 5? 1? 1 NA I want to select those columns for which I have the smallest number of NA In my example, these would be column1 and 2 since they give me 4??? 3 2??? 2 1??? 1 My real example have alot of columns and I want to find and save? all possible subsets Thanks, alot Evgenia -- View this message in context: http://r.789695.n4.nabble.com/Select-maximum-subset-tp4653845.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.
Hi, Just to make it more general. z1<-data.frame(z) z1[colSums(is.na(z1))<=min(colSums(is.na(z1[,-1])))] #? id? a? b #1? 1? 4? 3 #2? 2? 3 NA #3? 3? 2? 2 #4? 4 NA? 7 #5? 5? 1? 1 A.K. ----- Original Message ----- From: Evgenia <evgts at aueb.gr> To: r-help at r-project.org Cc: Sent: Sunday, December 23, 2012 4:16 PM Subject: [R] Select maximum subset Suppose i have matrix z id<-c(1,2,3,4,5) a <- c(4, 3, 2, NA, 1) b <- c(3, NA, 2, 7, 1) c <-c(3, NA, NA, 7, NA) z<- cbind(id,a, b,c) ? ? id? a? b? c [1,]? 1? 4? 3? 3 [2,]? 2? 3 NA NA [3,]? 3? 2? 2 NA [4,]? 4 NA? 7? 7 [5,]? 5? 1? 1 NA I want to select those columns for which I have the smallest number of NA In my example, these would be column1 and 2 since they give me 4??? 3 2??? 2 1??? 1 My real example have alot of columns and I want to find and save? all possible subsets Thanks, alot Evgenia -- View this message in context: http://r.789695.n4.nabble.com/Select-maximum-subset-tp4653845.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.