0

I wanna get the count of occurrences when the data in column 1 is 1 and also data in column 2 is 1. TIA

this

  • What have you tried? What was your result? Did you get an error? This is not a "please write my code for me" site, and questions showing little or no effort are less likely to obtain good answers. – Oliver Mar 17 '21 at 19:55
  • 1
    Your question is also more likely to get a good answer if you provide code and data that we can copy and past rather than images of data and code. See this question for how to create a good reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Ben Norris Mar 17 '21 at 19:57

2 Answers2

1

You can use filter and summarize from dplyr package

library(dplyr)

df1 <- df %>% 
  filter(col1 == 1 & col2==1) %>% 
  summarize(Freq_col1andcol2_equalto1 = n()) 

data:

df <- tribble(
  ~col1, ~col2,
  5,5,
  5,5,
  1,1,
  5,5,
  5,5)
TarJae
  • 72,363
  • 6
  • 19
  • 66
0
seed(323)
df <- tibble(x = round(runif(100, 0, 10)),
             y = round(runif(100, 0, 10)))

df %>% count(x==1,y==1)
 
#  A tibble: 4 x 3
#   `x == 1` `y == 1`     n
#   <lgl>    <lgl>    <int>
# 1 FALSE    FALSE       80
# 2 FALSE    TRUE        10
# 3 TRUE     FALSE        8
# 4 TRUE     TRUE         2

In this scenario, the first column gets 10 times number 1 (8 alone and 2 together with the second column), and the second column gets 12 times (10 alone and 2 together with the first column)

abreums
  • 166
  • 8