Sanchay Singh
Sanchay Singh

Reputation: 3

How to print a column grouped wrt another column in R Dataframe

I have a Data Frame df containing three columns namely, C1, C2 and C3.

     C1 C2 C3
1   1  O  g
2   2  E  f
3   3  O  e
4   4  E  d
5   5  O  g
6   6  E  e
7   7  O  f
8   8  E  h
9   9  O  i
10 10  E  g
11 11  O  i
12 12  E  h
13 13  O  e
14 14  E  f
15 15  O  h
16 16  E  h
17 17  O  d
18 18  E  h
19 19  O  f
20 20  E  f

Now i want to print values of C1 grouped with respect to C3 and after trying so many functions, i was not able to do the same. Is there any way to print that?

Upvotes: 0

Views: 37

Answers (1)

David Clarke
David Clarke

Reputation: 76

Do you mean df %>% group_by(C3) %>% count(C1)?

library(tidyverse)

C1 <- seq(1:20)
C2 <- c(rep(c("O", "E"), 10))
C3 <- c("g", "f", "e", "d", "g", "e", "f", "h", "i", "g", "i", "h", "e", "f", "h","h","d","h","f","f")
df <- data.frame(C1, C2, C3)

df %>% group_by(C3) %>% count(C1)

C3       C1     n
   <chr> <int> <int>
 1 d         4     1
 2 d        17     1
 3 e         3     1
 4 e         6     1
 5 e        13     1
 6 f         2     1
 7 f         7     1
...

Upvotes: 1

Related Questions