# How to Add a Count Column to a Data Frame in R

You can use the following basic syntax to add a ‘count’ column to a data frame in R:

```df %>%
group_by(var1) %>%
mutate(var1_count = n())
```

This particular syntax adds a column called var1_count to the data frame that contains the count of values in the column called var1.

The following example shows how to use this syntax in practice.

## Example: Add Count Column in R

Suppose we have the following data frame in R that contains information about various basketball players:

```#define data frama
df <- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'),
position=c('G', 'F', 'F', 'F', 'G', 'G', 'F', 'F'),
points=c(18, 22, 19, 14, 14, 11, 20, 28))

#view data frame
df

team position points
1    A        G     18
2    A        F     22
3    A        F     19
4    B        F     14
5    B        G     14
6    B        G     11
7    B        F     20
8    B        F     28
```

We can use the following code to add a column called team_count that contains the count of each team:

```library(dplyr)

#add column that shows total count of each team
df %>%
group_by(team) %>%
mutate(team_count = n())

# A tibble: 8 x 4
# Groups:   team 
team  position points team_count

1 A     G            18          3
2 A     F            22          3
3 A     F            19          3
4 B     F            14          5
5 B     G            14          5
6 B     G            11          5
7 B     F            20          5
8 B     F            28          5
```

There are 3 rows with a team value of A and 5 rows with a team value of B.

Thus:

• For each row where the team is equal to A, the value in the team_count column is 3.
• For each row where the team is equal to B, the value in the team_count column is 5.

You can also add a ‘count’ column that groups by multiple variables.

For example, the following code shows how to add a ‘count’ column that groups by the team and position variables:

```library(dplyr)

#add column that shows total count of each team and position
df %>%
group_by(team, position) %>%
mutate(team_pos_count = n())

# A tibble: 8 x 4
# Groups:   team, position 
team  position points team_pos_count

1 A     G            18              1
2 A     F            22              2
3 A     F            19              2
4 B     F            14              3
5 B     G            14              2
6 B     G            11              2
7 B     F            20              3
8 B     F            28              3```

From the output we can see:

• There is 1 row that contains A in the team column and G in the position column.
• There are 2 rows that contain A in the team column and F in the position column.
• There are 3 rows that contain B in the team column and F in the position column.
• There are 2 rows that contain B in the team column and F in the position column.
{"ft_lang_label":"__label__en","ft_lang_prob":0.811503,"math_prob":0.96379155,"size":2472,"snap":"2022-40-2023-06","text_gpt3_token_len":760,"char_repetition_ratio":0.16288492,"word_repetition_ratio":0.20267686,"special_character_ratio":0.33373785,"punctuation_ratio":0.08885018,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.970378,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-27T17:56:59Z\",\"WARC-Record-ID\":\"<urn:uuid:9ac097c5-778c-4535-91f1-4f7ba0da74a6>\",\"Content-Length\":\"50207\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8951129b-9caa-4f93-a1d2-ad424fa488f8>\",\"WARC-Concurrent-To\":\"<urn:uuid:86d99b3b-fcb1-458a-8455-748f1bf6cf4c>\",\"WARC-IP-Address\":\"35.206.100.4\",\"WARC-Target-URI\":\"https://www.statology.org/r-add-count-column/\",\"WARC-Payload-Digest\":\"sha1:B66XYOPV4RQ4CF2NURKA22VRNQJ5JKV7\",\"WARC-Block-Digest\":\"sha1:NWIWNP3C6ICHDR3R7CLUWDXOMXD5IWLY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764495001.99_warc_CC-MAIN-20230127164242-20230127194242-00852.warc.gz\"}"}