I'm having trouble figuring out how I can possibly group variables together. for example, I want to average all of candy1, where the data column ~COUNTRY is 'United States' or 'Canada' or 'United Kingdom'. while removing/omitting all rows that include NA(null?)
note: what I'm trying to do for the data frame has over 2000 rows and 140 columns.
Have considered using a for loop but haven't been able to figure out how to properly do it.
Candy_Hierarchy <- tribble(~COUNTRY, ~candy1, ~candy2, ~candy3,
'United States',2, 0, 1,
'United States', 1, 2, 0,
'United States',2, 1, 2,
'Canada', NA, NA, NA,
'Canada', 2, 0, 1,
'United Kingdom', 1, 2, 0)
into
Candy_Hierarchy <- tribble(~COUNTRY, ~candy1, ~candy2, ~candy3,
'US, Canada, and UK', 1.6, 1, 0.8,
'United States',1.67, 1, 1,
'Canada', 2, 0, 1,
'United Kingdom', 1, 2, 0)
'US, Canada, and UK' represents the total average of 'United States', 'Canada', and 'United Kingdom averaged as a whole.
don't really have any error messages as I haven't been able to figure out a way to make it happen.