You're almost there.
with(my.df, sum(my.df[A >= 1 & A < 3, "B"]))
EDIT
Chase challenged me to explain away the code at hand. When reading R code, it's best to read from inwards out. Here we go.
my.df
is a data.frame (think Excel sheet) with columns A
and B
. Square brackets []
are used to subset anything from this object like so: [rows, columns]
. For example, [1, ]
would return the entire first row, and if you add a column number (or column name), you get value in first row in that column (e.g. [1, 2]
, where you would get value in the first row of the second column).
We will now subset rows in my.df
with A >= 1 & A < 3
. What we're saying here is we want to see those rows that have values in A
bigger or equal to 1 and smaller than 3. This will give us all rows that satisfy this criterion. If we add , "B"
it means we want to select column B
. Since we already subsetted the result to contain only rows that fit the above criterion, by entering column name B
we get values only in the column. Once you have those values from column B
you sum them using sum()
.
Use of with
function is there to make our life easier. If we hadn't used that, we would be forced to call columns by their full name my.df$A
and my.df$B
.