Stututu1
Stututu1

Reputation: 1

Why are my calculated fields in an R data frame only creating new column names and not populating columns with calculated data?

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.

Upvotes: 0

Views: 38

Answers (1)

Nir Graham
Nir Graham

Reputation: 5167

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

Upvotes: 0

Related Questions