Hand holding pencil reviewing colorful data charts on desk with laptop.

Upload a Rely Column to a Information Body in R

Posted on
banner 336x280

You’ll be able to utility refer to unsophisticated syntax so as to add a ‘count’ column to an information body in R:

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

This actual syntax provides a column known as var1_count to the information body that accommodates the rely of values within the column known as var1.

banner 468x60

Refer to instance displays how one can utility this syntax in follow.

Instance: Upload Rely Column in R

Think we’ve refer to information body in R that accommodates details about diverse basketball gamers:

#outline information frama
df <- information.body(group=c('A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'),
                 place=c('G', 'F', 'F', 'F', 'G', 'G', 'F', 'F'),
                 issues=c(18, 22, 19, 14, 14, 11, 20, 28))

#view information body
df

  group place issues
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 will utility refer to code so as to add a column known as team_count that accommodates the rely of every group:

library(dplyr)

#upload column that displays general rely of every group
df %>%
  group_by(group) %>%
  mutate(team_count = n())

# A tibble: 8 x 4
# Teams:   group [2]
  group  place issues 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 group price of A and 5 rows with a group price of B.

Thus:

  • For every row the place the group is the same as A, the worth within the team_count column is 3.
  • For every row the place the group is the same as B, the worth within the team_count column is 5.

You’ll be able to additionally upload a ‘count’ column that teams by way of more than one variables.

As an example, refer to code displays how one can upload a ‘count’ column that teams by way of the group and place variables:

library(dplyr)

#upload column that displays general rely of every group and place
df %>%
  group_by(group, place) %>%
  mutate(team_pos_count = n())

# A tibble: 8 x 4
# Teams:   group, place [4]
  group  place issues 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 will see:

  • There’s 1 row that accommodates A within the group column and G within the place column.
  • There are 2 rows that include A within the group column and F within the place column.
  • There are 3 rows that include B within the group column and F within the place column.
  • There are 2 rows that include B within the group column and F within the place column.

Supplementary Sources

Refer to tutorials provide an explanation for how one can carry out alternative regular duties in R:

Staff By means of and Rely with Situation in R
Rely Choice of Parts in Checklist in R
Make a choice Distinctive Rows in a Information Body in R

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *