Home > Net >  Best way to count occurrences of one column based on another column
Best way to count occurrences of one column based on another column

Time:04-29

I have a dataframe with 4500 observations and 12 variables. My data is in arranged this way: Column 1 (n1) is an integer value (can be any of these- 10,30,70), column 2 (n2) is an integer value(can be any of these- 10,30,70), column 4 (mean2) is another integer value ( can be any of these-5,-1,0,1,5). I want to count the number of occurrences of one column based on the value of another column. For example, I want to find number of times column1=10 when column4= 5, then repeat it for different values of column4. I then want to plot a graph for column1=10, on x-axis- column4 values and y-axis- number of occurrences of colmun1. Thanks for the help.

dput(head(equal_var_df))
structure(list(n1 = c(10, 30, 70, 10, 30, 70), n2 = c(10, 10, 
10, 30, 30, 30), mean_1 = c(0, 0, 0, 0, 0, 0), mean_2 = c(0, 
0, 0, 0, 0, 0), var_1 = c(1, 1, 1, 1, 1, 1), var_2 = c(1, 1, 
1, 1, 1, 1), tpooled = c(-2.05010551964647, 0.652043624499215, 
1.48215219430884, 0.145893692582399, -0.983505294288748, 0.344138076925374
), pvalue_pooled = c(0.0552094923589302, 0.518297848331878, 0.1423287877924, 
0.884776497768807, 0.329443812298597, 0.731479781532784), result_pooled = c(1, 
1, 1, 1, 1, 1), t_unpooled = c(-2.05010551964647, 0.708471054370747, 
2.17657841099819, 0.127894927531869, -0.983505294288747, 0.346320053469156
), pvalue_unpooled = c(0.0561035624815491, 0.487727231028483, 
0.0438978780734246, 0.900350443079486, 0.329589054416284, 0.730423609249987
), result_unpooled = c(1, 1, 0, 1, 1, 1)), row.names = c(NA, 
-6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000023f500a1ef0>)

CodePudding user response:

require(tidyverse)
df %>%  
  count(n1, n2)
  • Related