0

I have data that is formatted like so. I would like to calculate the percent of enrollment associated with each racial group.

building <- c(1, 2)
total_enrollment <- c(100, 200)
black_count <- c(32, 69)
hispanic_count <- c(10, 19)
white_count <- c(44, 86)
asian_count <- c(5, 12)
nativeamerican_count <- c(4, 7)
multiracial_count <- c(5, 7)

school_racial_breakdown <- data.frame(building, total_enrollment, black_count, hispanic_count, white_count,
                                      asian_count, nativeamerican_count, multiracial_count)

I wrote the following code:

library(writexl)

cols <- c('black_count', 'hispanic_count', 'white_count', 'asian_count', 'nativeamerican_count', 'multiracial_count')

school_racial_breakdown[,paste0(cols, 'Percent')] <- lapply(cols, function(x) school_racial_breakdown[,x]/school_racial_breakdown[,2]) 

write_xlsx(school_racial_breakdown, 'Demographic File.xlsx')

However, when I write to the excel file, the columns containing the percentages are blank. Any idea why this is happening and how I can fix it?

Thank you!

ra_learns
  • 51
  • 6
  • what is `dem`? Is this supposed to be `school_racial_breakdown`? However, I do not get this error on my system when I change `dem` for `school_racial_breakdown` – jpsmith Nov 17 '22 at 16:13
  • Yes, sorry -- just corrected. – ra_learns Nov 17 '22 at 16:17
  • Hmm I still can't recreate this error on my system - seems like it may not be a coding issue. Try restarting your system/R/Rstudio. Or share `dput(school_racial_breakdown)` of the original data to give the exact structure – jpsmith Nov 17 '22 at 16:18
  • 1
    Ok, this really helped. When I used dput, I realized that maybe the values in the columns were not showing up because those columns were in list form, whereas all the other columns were vectors. So I did ```school_racial_breakdown[]<-lapply[school_racial_breakdown, unlist]``` and that resolved the issue. Thanks for being a thought partner - it helped! – ra_learns Nov 17 '22 at 18:23

1 Answers1

-1
require( tidyverse )
require( writexl )
counts <- tribble(
  ~black, ~hispanic, ~white, ~asian, ~native, ~multi
  ,   32,        10,     44,      5,       4,      5
  ,   69,        19,     86,     12,       7,      7
)

##  A tibble: 2 × 6
#   black hispanic white asian native multi
#   <dbl>    <dbl> <dbl> <dbl>  <dbl> <dbl>
# 1    32       10    44     5      4     5
# 2    69       19    86    12      7     7


# Total by building
( totals <- counts %>% rowSums() )
# [1] 100 200

( portions <- counts / totals )
#   black hispanic white asian native multi
# 1 0.320    0.100  0.44  0.05  0.040 0.050
# 2 0.345    0.095  0.43  0.06  0.035 0.035

portions %>% rowSums()
# [1] 1 1

( result <- cbind( counts, portions) )
#   black hispanic white asian native multi black hispanic white asian native multi
# 1    32       10    44     5      4     5 0.320    0.100  0.44  0.05  0.040 0.050
# 2    69       19    86    12      7     7 0.345    0.095  0.43  0.06  0.035 0.035

write_xlsx( result, 'Demographic File.xlsx')
Karl Edwards
  • 305
  • 4
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Nov 19 '22 at 15:56