I have three different treatments and within each one I have five sample values. I am into difficulties in how to sum the five values corresponding at each treatment.
Really I am working with a big dataframe which management is not too easy for me.
Below there is the dataframe
Add<-sapply(split(dataexample_2, dataexample_2$Treatment), function(d) sum(dataexample_2$Variable,na.rm = FALSE))
The problem with your code is that you refer to the whole data in your anonymus
function(d) instead of the current data
The following corrected line does what you want:
sapply(split(dataexample_2, dataexample_2$Treatment), function(d) sum(d$Variable,na.rm = FALSE))
Also I would suggest that you have a look at
tapply which is better suited for the task you want to accomplish than
sapply. It can do the whole thing much clearer:
tapply(dataexample_2$Variable, dataexample_2$Treatment, sum)