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

2008-04-02 Thread Johannes Hüsing
[EMAIL PROTECTED] [EMAIL PROTECTED] [Mon, Mar 31, 2008 at 07:02:25AM CEST]:
 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.

I always use apply instead of for when the steps can be performed 
independently and are parallelizable. By the same token as I use
map instead of recursion in Lisp.

R is essentially concealed Lisp, or so I'm told.

-- 
Johannes Hüsing   There is something fascinating about science. 
  One gets such wholesale returns of conjecture 
mailto:[EMAIL PROTECTED]  from such a trifling investment of fact.  
  
http://derwisch.wikidot.com (Mark Twain, Life on the Mississippi)

__
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.


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

2008-03-30 Thread Bill.Venables
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
x31   NA111115
x4   NA   NA   NA   NA   NA   NA   NA   NA



-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of zack holden
Sent: Monday, 31 March 2008 2:32 PM
To: r-help@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 each
row. 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)  ## make
dataframe
 
 
# 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@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@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.