user2924349 last month

r - dplyr: weighted sum of group returning multiple values error

In R, I have subwatersheds with % forest cover, and I want to find the % forest cover for the larger watershed comprised of these subwatersheds.

The data look like this:

df
          W_ID    sub_area    w_area   sub_forest 
             1        500      3045       15.5     
             1        650      3045       0     
             1        700      3045       32.4    
             1        645      3045       50.1    
             1        550      3045       16.9   
             2        480      2720       0.9   
             2        560      2720       18.5      
             2        760      2720       80.5 
             2        820      2720       18.6   

I tried to use dplyr

df %>%
   group_by(W_ID)%>%
   summarise(forest=(sum(sub_area*sub_forest)/w_area))

but it returns this error code

Error in summarise_impl(.data, dots) : expecting a single value

A similar approach using mutate doesn't return an error, but it returns n values of forest cover for n subwatersheds, whereas I want only m unique values of forest cover for m watershed IDs.

df %>%
  group_by(W_ID)%>%
  mutate(forest=(sum(sub_area*sub_forest)/w_area))

How do I properly use summarise to get % forest cover for each W_ID?

Answers


akrun last month

We need to change the brackets of sum to do the sum of proportion instead of sum of the products divided by 'w_area'

df %>% 
    group_by(W_ID) %>% 
    summarise(forest = sum(sub_area * sub_forest/w_area))
#    W_ID   forest
#  <int>    <dbl>
#1     1 23.65829
#2     2 32.06765

Post Status

Asked in last month
Viewed 1,034 times
Voted 14
Answered 1 times

Search




Leave an answer