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.