0

I'm doing an analysis in which I need to identify the number of land uses (mapbiomas, codes 1, 9, 15, 21...) according to the IDs of my plots. However, I can't think of a way to accomplish this.

enter image description here

I was hoping to find a way in which, based on the plot IDs, I could access the sum of the "areaHA" values ​​within each "mapbbiomas" code, something like this:

enter image description here

would you have any suggestions?

I tried grouping by ID and summarizing based on the sum of the areaHA values ​​that had mapbiomas code 1:

%>% group_by(ID) %>% summarise(sum(a.col.df$areaHa[a.col.df$mapbiomas=='1']))

however it returns me for all ID the same values

enter image description here

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Welcome to SO, Patrick Fernandes! Please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). Please include the code, console output, or data (e.g., `data.frame(...)` or the output from `dput(head(x))`) directly. – r2evans Mar 09 '23 at 20:46
  • 1
    It sounds like much of this is a merge/join operation, c.f., https://stackoverflow.com/q/1299871/3358272, https://stackoverflow.com/q/5706437/3358272. For dplyr, this means `*_join` functions. For the *"sum of the "areaHA" values"* part, I think it's likely https://stackoverflow.com/q/11562656/3358272 should be applicable. – r2evans Mar 09 '23 at 20:49

1 Answers1

0

Without having any dataset at hand, it is hard to say for sure. But I think you could group by both ID and biomass and then sum the area for each combination. Or do the plot IDs come from somewhere else?

df |> 
  group_by(ID, mapbiomas) |> 
  summarize(sum(areaHa))
dufei
  • 2,166
  • 1
  • 7
  • 18
  • that way I get a table that has 3 columns (IN, mapbiomas code and area). But I still can't get what I would like. Follow my data df <- data.frame( ID = c("1", "1","1","2","2","2","3","3","3","4","4","4","4"), mapbiomas = c("1","15","21","1","15","21","1","15","21","1","9","15","21"), areaHa = c("1000","15000","21000","10000","15000","21000","10000","15000","2100","10000","9000","15000","21000") ) df – Patrick Fernandes Mar 11 '23 at 18:22