If you redefine your NAs as below to be detected as some arbitrary large number, then the code should work through. Any 5's left in your dataset can be replaced just as easily by NAs again. Not elegant, but effective.
site <- c("s1", "s1", "s1", "s2","s2", "s2") pref <- c(1, 2, 3, 1, 2, 3) R1 <- c(NA, NA, 1, NA,NA,NA) R2 <- c(NA, 0, 1, 1, NA, 1) R3 <- c(NA, 1, 1, NA, 1, 1) R4 <- c(0, NA, 0, 1, NA, 0) R5 <- c(NA, 0, 1, NA, 1, 1) datum <- data.frame(site, pref, R1, R2, R3, R4, R5) ## For 1 column; datum$R1[is.na(datum$R1)==T]<-5 tapply(datum$R1, datum$site, min, na.rm=T) ## Can loop this over all columns; new<-matrix(0,5,2) for (i in 3:7) {datum[,i][is.na(datum[,i])==T]<-5 new[i-2,]<-tapply(datum[,i], datum$site, min, na.rm=T)} -- View this message in context: http://r.789695.n4.nabble.com/information-reduction-database-management-question-tp2278863p2279385.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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.