How can I convert this apply function in R studio
apply(trees, 1, function(x) tree_s3(x[1],x[2],x[3]))
into a for loop?
You can use for loop by iterating from 1 to the number of rows. Then access each row using the index as shown below.
We are iterating using rows because the margin argument is 1 in this apply statement.
Although I don't have the definition of what tree_s3( ) is doing so I cannot show you the exact output for this function. You can share the full code in the comments if you want me to show the exact conversion.
for i in 1:nrow(trees){
x <- trees[ i, ]
#Your function here using x as the argument
}
(*Note: Please up-vote. If any doubt, please let me know in the comments)
Get Answers For Free
Most questions answered within 1 hours.