KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
R textbooks continue to promote the use of lapply instead of loops. This is easy even for functions with arguments like lapply(somelist, f, a=1, b=2) but what if the arguments change depending on the list element? Assume my somelist consists of: somelist$USA somelist$Europe somelist$Switzerland plus there is anotherlist with the same regions and I want use lapply with these changing arguments? This could be useful when f was a ratio calculation for example. lapply(somelist, f, a= somelist$USA, b=anotherlist$USA) Is there are way except for a loop to run through these regions efficiently? EDIT: my problem seems to be that I tried to use a previously written function without indexes... ratio <-function(a,b){ z<-(b-a)/a return(z) } which led to lapply(data,ratio,names(data)) which does not work. Maybe others can also learn from this mistake.
Tags (comma-separated)
Save Edits
Cancel