'append values in for loop R as in python

I am trying to reproduce the following functions created in Python in R.

# Python 
def square_area(side):
    return side * side

results = []
for i in range(1, 10):
    x = square_area(i)
    results.append(x)
print results

outcome

[1, 4, 9, 16, 25, 36, 49, 64, 81]

my attempt in R has been:

# R
square_area <- function(side) {
side * side
}

results=list()
for (i in 1:10){ 
x <- square_area(i)
results[i] = x 
}
print(results)

outcome

[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25

[[6]]
[1] 36

[[7]]
[1] 49

[[8]]
[1] 64

[[9]]
[1] 81

[[10]]
[1] 100

I don't know if this is correct but I need the result as a list to build later on a line graph. This seams to be more like a python dictionary with keys and values. How do you simply append values in R?

Thanks.



Solution 1:[1]

We can get this directly by doing the ^ on the vector

 (1:10)^2
 #[1]   1   4   9  16  25  36  49  64  81 100

If you need a list, just wrap it with as.list

 as.list((1:10)^2)

Solution 2:[2]

It's completely FALSE that a list in Python is the same as a vector in R.

A list can save many different kinds of values, vectors can't, just as matrices.

in Python you can do the list:

[1, [2, 3.333], "I'm a string bitch!", [1, "hollymolly"]]

in R that's impposible to do with a vector. You use a List to do that. That´s why is called a List.

I´ve been searching the same thing as you through the Web, and seem like useRs have not the necessity to employ Lists as Pythonistas does, and what's worst useRs confuse the meaning of the object List with a Vector, as if they where equivalent (FALSE).

A vector in R is much like an np.array in Python. That´s why R is cool, it doesn't require a Package to work with matrices.

What you can do in your particular example is as follows (read the comments):

#R
Area2 <- function(side){   #This is your function
  side^2
}

# The truth is that in your example, is enough to employ a vector and add stuff to it. 
# That's why I will make a slightly more complex code to append shit to a list and show my point
LIST = list()

for(i in 1:2){
  vect = c()
  for(j in 1:10){
    vect = c(vect, i * Area2(j))
  }
  LIST[[i]] = vect  #This is the climax of the whole story (how to append to a List)
}

print(LIST)

Outcome:

[[1]]
 [1]   1   4   9  16  25  36  49  64  81 100

[[2]]
 [1]   2   8  18  32  50  72  98 128 162 200

Then you can just employ each Value in the List as you wish, like for the plot you just required.

Hope I helped.

Solution 3:[3]

The append function from base works on lists.

a = list()
a = append(a, "one")
a = append(a, 2)

It is a normal list:

> print(a)
[[1]]
[1] "one"

[[2]]
[1] 2

>lapply(a, "class")
[[1]]
[1] "character"

[[2]]
[1] "numeric"

A quirk is that if you pass a vector (longer than a singleton) as the second argument (values), R will do like Python's extend, i.e. it will create new slots for each element of the vector. Fix this by wrapping the vector in with list(). So:

>append(a, c(3, 4))
[[1]]
[1] "one"

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

>append(a, list(c(3, 4)))
[[1]]
[1] "one"

[[2]]
[1] 2

[[3]]
[1] 3 4

A downside of this is that you are forced to use a single type by virtue of passing the values through c(). If you want to use mixed types within elements, there are probably easier approaches without append, but in principle you could iterate back through the list once it is all set and recast the elements to the desired types.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akrun
Solution 2 Alejo
Solution 3