[R] Suprising R behaviour

David L Carlson dcarlson at tamu.edu
Sat Nov 14 17:04:06 CET 2015


You definitely need to learn about the differences between R and the other languages that you are familiar with. You've done some work since you mention the apply() family, but those are just another way of programming a loop. In many cases (including this one), a loop is not needed. Here's your code in a plain text message with some additions:

data <- data.frame(x = c(1,2,3,1,1,1), y = c(1,2,3,4,6,7))
# fin_hyp could just as easily be a data frame or a matrix
fin_hyp <- list(slope = 2, constant = 1)
# R vectorizes the following command and automatically computes the
# result for each row in "data"
outputs <- data['y'] > fin_hyp['slope'] * data['x']  + fin_hyp['constant']
outputs
# Add a plot showing points above and below the line
# ifelse is vectorized so it creates a vector with
# 16 (symbol for solid circle) if above the line and 
# 1 (open circle) if below the line
sym <- ifelse(outputs, 16, 1)
plot(y~x, data, pch=sym)
abline(a=fin_hyp$constant, b=fin_hyp$slope)


David L. Carlson
Department of Anthropology
Texas A&M University

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Duncan Murdoch
Sent: Friday, November 13, 2015 1:16 PM
To: Ilgaz S <ilgaz.somer at gmail.com>; r-help at r-project.org
Subject: Re: [R] Suprising R behaviour

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.

______________________________________________
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