I have some lines of code with a for loop that look like this:
somevector2 <- c(length = somevector2_length)
for(string in somevector1){
df2 <- df1[df1$col1 == string, ]
ff <- somefunction(df2$col2)
somevector2 <- c(somevector2, ff)
}
From what i understood initializing the vector with the correct length should make the loop faster, but it still takes quite sometimes although the somefunction(df2$col2) does some simple operations.somevector1 it's just a vector of strings
Is there a way to make this loop faster in R? thank you very much