Global averaging by masking

Dear all,

I am trying to compute global land only average area facing extreme temperature (>35degree). Can you please help me improve the code>

r= brick("Temperature_1961_2024.nc")
m=raster("mask_ocean.nc") # land=1, ocean=0
S=r*m # masked ocean
A=area(S) *m # area with ocean masked
At=cellStats(A, "sum") # total land area

Ss=S > 35 # mask area with temperature >35

dr=area(Ss) *Ss # Areas with temperature >35 * gridarea

drt=cellStats(dr, "sum")
pr=(drt/At) # fraction of land with > 35
pr*100 # percentage

Thank you!!!