From: <Bill.Venables_at_csiro.au>

Date: Mon, 31 Mar 2008 15:02:25 +1000

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.

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. Received on Mon 31 Mar 2008 - 05:12:00 GMT

Date: Mon, 31 Mar 2008 15:02:25 +1000

As far as I know there is no function called 'APPLY'

There is one called 'apply', but why are you determined to use it here? It is essentially concealed looping.

Here is an alternative way of solving your problem: ___

x <- rbind(x1 = c(1, NA, NA, NA, NA, 1, 2, 2), x2 = c(1, NA, NA, NA, NA, 1, 2, 1), x3 = c(1, NA, 1, 1, 1, 1, 1, 5), x4 = c(1, NA, NA, NA, NA, NA, NA, 5))

badRows <- which(rowSums(is.na(x)) > 3)

is.na(x[as.matrix(expand.grid(badRows, 1:ncol(x)))]) <- TRUE
___

*> x
*

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
x1 NA NA NA NA NA NA NA NA
x2 NA NA NA NA NA NA NA NA
x3 1 NA 1 1 1 1 1 5
x4 NA NA NA NA NA NA NA NA

*>
*

-----Original Message-----

From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org]
On Behalf Of zack holden

Sent: Monday, 31 March 2008 2:32 PM

To: r-help_at_r-project.org

Subject: [R] APPLY as alternate to FOR loop?

Dear list,

Below I've written a clunky for loop that counts NA's in a row,
replacing all with NA if there are

more than 3 missing values, or keeping the values if 4 or more are
present. This is sample code from a very large
dataframe I'm trying to clean up.

I know there are many simpler more elegant solutions to this little problem.

Would someone be willing to show me how to create a function that I can
APPLY to

each row rather than looping? I've tried and can't get it.

Thank you,

Zack

################################################# Count NA's in eachrow. IF > 3 NA's in a row, make all

NA############################################################# test dataframe #######x1 <- c(1,NA,NA,NA,NA,1,2,2)x2 <- c(1,NA,NA,NA,NA,1,2,1)x3 <- c(1,NA,1,1,1,1,1,5)x4 <- c(1,NA,NA,NA,NA,NA,NA,5)x <- rbind(x1,x2,x3,x4)test <- rowSums(is.na(x))

## count numer of NA's in rowx <- cbind(x, test) ## add row NA count to datax <- data.frame(x) ## makedataframe

# FOR LOOP to apply across all rows of dataframe -- for(i in 1:nrow(x)) {if(x[i,9] > 4) {x[i,1:7] <- NA} else { x[i,1:7] <- x[i,1:7]i = i+1}}

##################################################### [[alternative HTML version deleted]] ______________________________________________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.

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. Received on Mon 31 Mar 2008 - 05:12:00 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Wed 02 Apr 2008 - 20:31:07 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*