Summing certain parts of columns in R studio

How is the number of cases / number of controls distributed with respect to the age categories?

how would I add row number 1-15 in "ncontrols" column?

Welcome to the forum,

Screenshots are not very useful but here is a simple example .

dat1 <- data.frame(aa = sample(1:50, 20), bb = LETTERS[1:20])                              
sum(dat1[1:15, "aa"])

You might find this useful: FAQ

what do the "aa" and "bb" represent?

I have named my data set, as it is question 1's data.
I want to sum the first 15 rows of data in the "ncontrols" or the 5th column

so should my code read:
dat1 <- data.frame(aa =, 5), **bb = LETTERS[1:20] (???) **) (???)
sum(dat1[1:15, "aa"])

They are column names, corresponding to "agegp" or "ncontrols", etc.

No, let's say your data set is a data.frame called "mydata". All you need to do is

sum(mydata[1:15, "ncontrols"]  

In English, this means we are selecting the column "ncontrols" and taking rows from 1 to 15 and summing those 15 numbers.

You might find the Wrangling and Data chapters of Preceptor’s Primer a handy intro.

Hi, for a reproducible example instead of a screenshot:

For grouping and summarising:

Summarise each group down to one row — summarise • dplyr (

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.