Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding values to the vector inside for loop in R

I have just started learning R and I wrote this code to learn on functions and loops.

squared<-function(x){
  m<-c()
  for(i in 1:x){
    y<-i*i
    c(m,y)
  }
  return (m)
}
squared(5)  

NULL  

Why does this return NULL. I want i*i values to append to the end of mand return a vector. Can someone please point out whats wrong with this code.

like image 494
sam_rox Avatar asked Dec 14 '22 12:12

sam_rox


1 Answers

You haven't put anything inside m <- c() in your loop since you did not use an assignment. You are getting the following -

m <- c()
m
# NULL

You can change the function to return the desired values by assigning m in the loop.

squared <- function(x) {
    m <- c()
    for(i in 1:x) {
        y <- i * i
        m <- c(m, y)
    }
    return(m)
}

squared(5)
# [1]  1  4  9 16 25

But this is inefficient because we know the length of the resulting vector will be 5 (or x). So we want to allocate the memory first before looping. This will be the better way to use the for() loop.

squared <- function(x) {
    m <- vector("integer", x)
    for(i in seq_len(x)) {
        m[i] <- i * i
    }
    m
}

squared(5)
# [1]  1  4  9 16 25 

Also notice that I have removed return() from the second function. It is not necessary there, so it can be removed. It's a matter of personal preference to leave it in this situation. Sometimes it will be necessary, like in if() statements for example.

I know the question is about looping, but I also must mention that this can be done more efficiently with seven characters using the primitive ^, like this

(1:5)^2
# [1]  1  4  9 16 25

^ is a primitive function, which means the code is written entirely in C and will be the most efficient of these three methods

`^`
# function (e1, e2)  .Primitive("^")
like image 108
Rich Scriven Avatar answered Jan 06 '23 00:01

Rich Scriven