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: 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]]
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.
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.
My original answer works fine as long as fin_hyp contains only one definition for the line, but if it contains more than one, R will use a different line for each test which is probably not what you want (e.g. the first point will be tested against the first line and the second point against the second line, etc). In that case a loop or apply function would be needed:> data <- data.frame(x = c(1,2,3,1,1,1), y = c(1,2,3,4,6,7)) > fin_hyp <- data.frame(slope = c(2, 1, -2), constant = c(1, -1, 7)) > outputs <- apply(fin_hyp, 1, function(z) data$y > z[1] * data$x + z[2]) > outputs[,1] [,2] [,3] [1,] FALSE TRUE FALSE [2,] FALSE TRUE FALSE [3,] FALSE TRUE TRUE [4,] TRUE TRUE FALSE [5,] TRUE TRUE TRUE [6,] TRUE TRUE TRUE The first column is the result for the first equation (row in fin_hyp) and so on. David L. Carlson Department of Anthropology Texas A&M University -----Original Message----- From: David L Carlson Sent: Saturday, November 14, 2015 9:57 AM To: 'Duncan Murdoch' <murdoch.duncan at gmail.com>; Ilgaz S <ilgaz.somer at gmail.com>; r-help at r-project.org Subject: RE: [R] Suprising R behaviour 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.