I'm fairly new to R programming. Can someone say why this code keeps giving an error : paste0("emissions_for_",yr) <- sum(nei_tst[,var][select_obs], na.rm=TRUE)
- (nei_tst is a dataframe)
- (var is a variable that was assigned the name of one column in that dataframe)
- select_obs is a variable with logical elements (result of test : yr == "1999")
I get the foll. error : Error in paste0("emissions_for_", yr) <- sum(nei_tst[, var][select_obs], :
target of assignment expands to non-language object
tapply(nei_tst[,var], nei_tst$yr, sum, na.rm=TRUE)will give you the result you want. Building variable names like they are strings is just not a good idea in R and often makes things much more difficult to work with. I strongly suggest you avoid this strategy.