Home > Software engineering >  Cannot calculate percentage?
Cannot calculate percentage?

Time:10-30

I'm trying to calculate the percentage of column but the function did not work.

Here is my dataset: enter image description here

any help would be greatly appreciated!

CodePudding user response:

Try ungrouping after summarising the data. Then yes, compute the percentages.

suppressPackageStartupMessages(
  library(tidyverse)
)

sale %>% 
  filter(!is.na(`Property Type`)) %>%  
  group_by(`Property Type`) %>% 
  summarize(sale_vol = n()) %>%
  ungroup() %>%
  mutate(percent = sale_vol/sum(sale_vol)*100) 
## A tibble: 12 × 3
#   `Property Type`  sale_vol   percent
#   <chr>               <int>     <dbl>
# 1 ""                 382446 38.4     
# 2 "Apartments"          486  0.0487  
# 3 "Commercial"         1981  0.199   
# 4 "Condo"            105420 10.6     
# 5 "Four Family"        2150  0.216   
# 6 "Industrial"          228  0.0229  
# 7 "Public Utility"        5  0.000501
# 8 "Residential"       60728  6.09    
# 9 "Single Family"    401612 40.3     
#10 "Three Family"      12586  1.26    
#11 "Two Family"        26408  2.65    
#12 "Vacant Land"        3163  0.317   
  • Related