How to find the groupwise number of positive and negative values in an R data frame?

To find the groupwise number of positive and negative values in an R data frame, we can use mutate function of dplyr package. For example, if we have a data frame called df with one categorical column x and one numerical column y then the number of positive and negative values for categorical column can be found by using the below command −

df%>%group_by(x)%>%mutate(positive=sum(y>0),negative=sum(y<0))

Consider the below data frame −

Example

Live Demo

Group<-sample(LETTERS[1:4],20,replace=TRUE)
y1<-rnorm(20)
df1<-data.frame(Group,y1)
df1

Output

  Group  y1
1  C    0.85135509
2  D   -1.65551392
3  C   -3.02201905
4  B   -0.89625797
5  A    0.28129292
6  A   -0.04801872
7  D   -0.95801637
8  B    1.42274072
9  A   -1.18959023
10 A    0.60031795
11 C    0.22474189
12 B    2.17627556
13 D    1.24592142
14 A   -0.46659749
15 B    0.43163726
16 D   -0.95450596
17 C    2.89909436
18 D    0.55951749
19 C   -0.48761069
20 B    0.85106755

Loading dplyr package and finding groupwise number of positive and negative values for Group column in df1 −

Example

library(dplyr)
df1%>%group_by(Group)%>%mutate(positive=sum(y1>0),negative=sum(y1<0))

A tibble: 20 x 4

Groups: Group [4]

Output

   Group y1 positive negative
<chr> <dbl> <int> <int>
1  C    0.851   3     2
2  D   -1.66    2     3
3  C   -3.02    3     2
4  B   -0.896   4     1
5  A    0.281   2     3
6  A   -0.0480  2     3
7  D   -0.958   2     3
8  B    1.42    4     1
9  A   -1.19    2     3
10 A    0.600   2     3
11 C    0.225   3     2
12 B    2.18    4     1
13 D    1.25    2     3
14 A   -0.467   2     3
15 B    0.432   4     1
16 D   -0.955   2     3
17 C    2.90    3     2
18 D    0.560   2     3
19 C   -0.488   3     2
20 B    0.851   4     1

Example

Live Demo

Class<-sample(c("Male","Female"),20,replace=TRUE)
y2<-rnorm(20)
df2<-data.frame(Class,y2)
df2

Output

    Class        y2
1    Male    0.05278402
2  Female    1.61835971
3  Female    1.17452205
4    Male   -0.61445588
5  Female   -0.25727985
6  Female    0.75821980
7    Male   -1.08398301
8    Male    0.77650504
9    Male    0.54157874
10   Male    0.42124703
11   Male    0.36434790
12   Male   -1.03566231
13   Male    0.06208824
14   Male    0.11831360
15   Male    1.22460403
16   Male   -2.43157148
17   Male   -0.50869299
18   Male   -1.14549469
19 Female  1.45219635
20   Male   -1.14271790

Finding groupwise number of positive and negative values for Class column in df2 −

Example

df2%>%group_by(Class)%>%mutate(positive=sum(y2>0),negative=sum(y2<0))
# A tibble: 20 x 4
# Groups: Class [2]

Output

   Class y2 positive negative
<chr>     <dbl>  <int>   <int>
1   Male   0.0528  8       7
2 Female   1.62    4       1
3 Female   1.17    4       1
4   Male  -0.614   8       7
5 Female  -0.257   4       1
6 Female   0.758   4       1
7   Male  -1.08    8       7
8   Male   0.777   8       7
9   Male   0.542   8       7
10  Male   0.421   8       7
11  Male   0.364   8       7
12  Male  -1.04    8       7
13  Male   0.0621  8       7
14  Male   0.118   8       7
15  Male   1.22    8       7
16  Male  -2.43    8       7
17  Male  -0.509   8       7
18  Male  -1.15    8       7
19 Female 1.45     4       1
20  Male  -1.14    8       7

Updated on: 06-Feb-2021

535 Views