1

I'm new to programming in R and i think this is super basic but I couldn't find another question like it. So in my df the middle column is binary and either I or D and the last column is either FA, PS, CR, DI, HD. I'm trying to count the amount of times each occurs with one another, for example how many times I occurs with CR.

  1          I                  CR
  2          D                  DI
  3          D                  DI
  4          D                  PS
  5          D                  PS
  6          D                  PS
  7          D                  DI
  8          D                  CR
  9          I                  FA
  10         D                  CR

I counted the totals of HD, PS, FA etc using factor then putting it into a table and so i tried doing this with conditionals for the two columns together but couldn't get it to work. If anyone could help it would be nice.

ckh98
  • 11
  • 2

2 Answers2

1

You can use table

> with(df, table(V2, V3))
   V3
V2  CR DI FA PS
  D  2  3  0  3
  I  1  0  1  0

your df is:

> df
   V1 V2 V3
1   1  I CR
2   2  D DI
3   3  D DI
4   4  D PS
5   5  D PS
6   6  D PS
7   7  D DI
8   8  D CR
9   9  I FA
10 10  D CR
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
0

An option is count

library(dplyr)
df1 %>% 
  count(V2, V3)
akrun
  • 874,273
  • 37
  • 540
  • 662