title: R: Counting Overall Percentage of 0's in Data
body: <p>I am working with the R programming language.</p>
  <p>In the following link (<a href="https://www.geeksforgeeks.org/how-to-find-the-percentage-of-missing-values-in-a-dataframe-in-r/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/how-to-find-the-percentage-of-missing-values-in-a-dataframe-in-r/</a>), I found out a method to calculate the total percentage of NAs in a data frame :</p>
  <pre><code># declaring a data frame in R
  data_frame = data.frame(C1= c(1, 2, NA, 0),
                          C2= c( NA, NA, 3, 8), 
                          C3= c(&quot;A&quot;, &quot;V&quot;, &quot;j&quot;, &quot;y&quot;),
                          C4=c(NA,NA,NA,NA))
    
  percentage = mean(is.na(data_frame)) * 100
  
  [1] 43.75
  </code></pre>
  <p><strong>My Question:</strong> Is there a way to extend this to count the percentage of &quot;any element&quot; in the data frame?</p>
  <p>For example, can this be used to calculate the percentage of 0s in the data set? Or the percentage of times &quot;j&quot; appears in the data? Or the percentage of times &quot;2&quot; appears in the data set?</p>
  <p>I can do this manually:</p>
  <pre><code># count percentage of &quot;j&quot; in the data 
  
  v1 = nrow(subset(data_frame, C1 == &quot;j&quot;)) 
  v2 = nrow(subset(data_frame, C2 == &quot;j&quot;))
  v3 = nrow(subset(data_frame, C3== &quot;j&quot;)) 
  v4 = nrow(subset(data_frame, C4 == &quot;j&quot;))
  
  percentage = ((v1 + v2 + v3 + v4) / ((nrow(data_frame) * ncol(data_frame)))) * 100
  
  [1] 6.25
  
  # count percentage of &quot;0&quot; in the data  (I don't think this is right, it should be written as &quot;nrow(subset(data_frame, C1 &lt;= 0))&quot;?)
  
  v1 = nrow(subset(data_frame, C1 = 0)) 
  v2 = nrow(subset(data_frame, C2 = 0))
  v3 = nrow(subset(data_frame, C3= 0)) 
  v4 = nrow(subset(data_frame, C4 = 0))
  
  percentage = ((v1 + v2 + v3 + v4) / ((nrow(data_frame) * ncol(data_frame)))) * 100
  </code></pre>
  <p>But is there a faster way to do this?</p>
  <p>Thanks!</p>