简体   繁体   中英

R mean aspect calculation (circular statistics)

I have aspect data from DEM pixels forming individual areas. I would like to calculate the mean aspect for each polygon using this data. I wish to apply the function to every row of my df and store it in the 'Mean_Aspect' column.

My dataframe looks like this: DF示例

The code I have so far calculates the mean aspect if I copy an paste csv into it, but I cannot work out how to loop/apply to all. Values in 'angles' are example onkly.

data$Mean_Aspect <- 0
library(circular)
angles = c(200.072,204.037,198.591,193.151,192.779,187.503,198.549,196.675,199.618,191.083,187.242)
anglecir =  circular(angles, type="angles", units="degrees",modulo="2pi", template='geographics')
mean(anglecir)

Any help/advice appreciated!

如果度数列包含要计算的角度 ,则可以尝试:

data$Mean_Aspect <- lapply(data$degrees, function(angles) mean(circular(angles, type="angles", units="degrees",modulo="2pi", template='geographics')))

We can use sapply

data$Mean_Aspect <- sapply(data$Degrees, function(angles) 
   mean(circular(angles, type="angles", units="degrees",modulo="2pi", 
                template='geographics')))
data$Mean_Aspect
#[1] 129.184797   6.358874 243.757731 128.159000

data

data <- data.frame(FID = 0:3, Degrees = I(list(c(120.53, 133, 854), 
          c(338.629, 331.991, 323.4, 133.2, 432), 
         c(251.114, 248.003, 232.1), c(121.992, 134.326))), 
      Mean_Aspect = 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM