0

This is the code I am using to create a new calculated field for the proportion of a race in a total population:

formatted_df$White_Proportion <- formatted_df$White_Alone / formatted_df$Total_Pop1

When running the code the data frame includes new columns for the calculated field, but they are not populated with the values associated with the calculation. The entire column is null.

I tried to change syntax to no avail. Have attempted to find a similar issue online but have been unable to. Help would be appreciated.

thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • 2
    Welcome to Stack Overflow. You'll get better answers if you [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including a small representative dataset in a plain text format - for example the output from `dput(formatted_df)`, if that is not too large. Looking at your data, can you see a reason why the output of the division might be NULL (and is it really NULL) ? – neilfws Jun 21 '23 at 00:46
  • 4
    What does the division return (just run it in the console instead of adding it to the data frame)? Are both columns type numeric (run `str` on your data frame)? Are there any NAs or 0 values? Without a minimal example of your data, there’s no way to answer this specifically – divibisan Jun 21 '23 at 00:50

1 Answers1

0

Your code would be expected to work if your data was reasonably constituted. i.e.

formatted_df <-data.frame(
  White_Alone = 1 ,
  Total_Pop1=2
)

formatted_df$White_Proportion <- formatted_df$White_Alone / formatted_df$Total_Pop1

formatted_df
 White_Alone Total_Pop1 White_Proportion
          1          2              0.5

Therefore a reprex would be required to address your specific data issues

Nir Graham
  • 2,567
  • 2
  • 6
  • 10
  • Thanks for the tip, I realized I was including an unnecessary column in my data frame that was messing with the calculation. – Stututu1 Jun 22 '23 at 17:35