Find the group wise large and small values in an R data frame.

R ProgrammingServer Side ProgrammingProgramming

To find the groupwise large and small values in an R data frame, we can use mutate function of dplyr package, the grouping can be easily done with the help of group_by function.

For Example, if we have a data frame called df that contains grouping column say Grp and a numerical column Num then we can find the groupwise large and small values by using the below command −

df%%group_by(Gp)%%mutate(Val=ifelse(Val==max(Val),"large","small"))

Example 1

Following snippet creates a sample data frame −

Group<-rep(LETTERS[1:10],each=2)
Dep_Var<-rpois(20,5)
df1<-data.frame(Group,Dep_Var)
df1

The following dataframe is created

Group Dep_Var
1  A  11
2  A   5
3  B   2
4  B   2
5  C  10
6  C   8
7  D   6
8  D   5
9  E   3
10 E   5
11 F   3
12 F   8
13 G   8
14 G   2
15 H   3
16 H   3
17 I   7
18 I   5
19 J   3
20 J   5

To load dplyr package and finding large and small values of Dep_Var for each group in Group column of df1 on the above created data frame, add the following code to the above snippet −

Group<-rep(LETTERS[1:10],each=2)
Dep_Var<-rpois(20,5)
df1<-data.frame(Group,Dep_Var)
library(dplyr)
df1%%group_by(Group)%%mutate(Magnitude=ifelse(Dep_Var==max(Dep_Var),"large","small"))
# A tibble: 20 x 3
# Groups: Group [10]

Output

If you execute all the above given snippets as a single program, it generates the following Output −

  Group Dep_Var Magnitude
   <chr    <int   <chr
1    A     11    large
2    A      5    small
3    B      2    large
4    B      2    large
5    C     10    large
6    C      8    small
7    D      6    large
8    D      5    small
9    E      3    small
10   E      5    large
11   F      3    small
12   F      8    large
13   G      8    large
14   G      2    small
15   H      3    large
16   H      3    large
17   I      7    large
18   I      5    small
19   J      3    small
20   J      5    large

Example 2

Following snippet creates a sample data frame −

Class<-rep(1:10,each=2)
Score<-sample(20:50,20)
df2<-data.frame(Class,Score)
df2

The following dataframe is created

Class Score
1   1   44
2   1   25
3   2   40
4   2   30
5   3   34
6   3   37
7   4   36
8   4   27
9   5   38
10  5   24
11  6   29
12  6   20
13  7   47
14  7   35
15  8   45
16  8   21
17  9   49
18  9   46
19 10   31
20 10   48

To find large and small values of Score for each group in Class column of df2 on the above created data frame, add the following code to the above snippet −

Class<-rep(1:10,each=2)
Score<-sample(20:50,20)
df2<-data.frame(Class,Score)
df2%%group_by(Class)%%mutate(Magnitude=ifelse(Score==max(Score),"large","small"))
# A tibble: 20 x 3
# Groups: Class [10]

Output

If you execute all the above given snippets as a single program, it generates the following Output −

Class Score Magnitude
<int <int   <chr
1  1 44    large
2  1 25    small
3  2 40    large
4  2 30    small
5  3 34    small
6  3 37    large
7  4 36    large
8  4 27    small
9  5 38    large
10 5 24    small
11 6 29    large
12 6 20    small
13 7 47    large
14 7 35    small
15 8 45    large
16 8 21    small
17 9 49    large
18 9 46    small
19 10 31   small
20 10 48   large
raja
Updated on 05-Nov-2021 08:01:46

Advertisements