[R] Suprising R behaviour

Duncan Murdoch murdoch.duncan at gmail.com
Fri Nov 13 20:15:35 CET 2015


On 13/11/2015 8:11 AM, Ilgaz S wrote:
> Hello everybody, I am new to R and I discovered something that suprise me
> and I have a question about it.
> Today I wanted to return a bit array which represents this:
>
> if( arbitrary point above the line)
>       return TRUE
> else
>       return FALSE
>
> First I tought I would use for loop and access every element of the data.
> Then I tend to use lapply function.
>
> At the end, I accidently done that without using any if/else statement. (
> or for loop )  Here is the code:

I can't read your code (you posted in HTML, don't do that), but it 
sounds as though you have discovered vectorized operations.  These are 
central to good R programming, and are well described in the 
Introduction to R manual.

Duncan Murdoch
>
> data <- data.frame(x= c(1,2,3,1,1,1), y = c(1,2,3,4,6,7))fin_hyp <-
> list(slope=2,constant=1)outputs <- data['y'] > fin_hyp['slope'] *
> data['x']  +fin_hyp['constant']outputs
>
> What is R doing here? It is using loop somewhere inside? Is this code
> more efficient than other methods I mentioned?
>
> Thank you, I.S.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list