I need to subtract specified value from each list element in R. In this article is mentioned that for such tasks the apply
family of functions are used instead of loops. I've tried following:
# Define list
> a = 1:20
# Substraact this from each element
> substract_me = 5
# Function for substracting
> substract = function(x,y) { ret = x-y; return(ret) }
# The problem is that I do not know how to access the current array element and pass it to substract function
lapply(a, substract)
Here is mentioned that the anonymous functions can be also used, but it did not worked for me and I get syntax error. Indeed it looks to me just like syntactic sugar. The problem remain the same, that I need some placeholder or something when I am using lapply
function so I can access the current list element:
lapply(a, function([WHAT TO ADD HERE???],substract_me) substract([WHAT TO ADD HERE???],substract_me))
Here is something probably related but I did not figure out how stuff works from posted code snippets.