首页 > 解决方案 > 当我尝试应用熵计算时,如何解决二进制运算符错误的非数字参数?

问题描述

当我尝试对我的数据应用熵计算时出现错误。
这是一个使用示例数据的示例:

#Non-uniform entropy-esque measure
   

#Example Data
id <- c(0,2,3,4,5,6,7)
Region_ID <- c(0,2,2,3,0,3,2)
BaseunitPOP <- c(10000,4000,8000,8000,10000,11000,13000)
RegionPOP<- c(20000,25000,25000,19000,20000,19000,25000)

S.data <- data.frame(id, Region_ID, BaseunitPOP,RegionPOP)

       
#For each region, calculate non-uniform entropy-type measure
#Or skip to the bottom of script to calculate for ALL regions at once

#region 1
#-(10000*log((10000/20000),2)+(10000*log((10000/20000),2)))

#region 2
#-(4000*log((4000/25000),2)+(8000*log((8000/25000),2))+(13000*log((13000/25000)))

#region 3
#...

######################
#For ALL Regions

S.data %>% group_by(Region_ID) %>%
  summarise(result=-sum(BaseunitPOP*log((BaseunitPOP/RegionPOP),2)))

当我尝试将其应用于我的数据时(使用下面的代码调用我的数据样本):

structure(list(region_ID = c(0, 0, 0, 0, 0, 0), TOTPOP_CY = c(1286, 
    995, 838, 628, 719, 974), `sum(TOTPOP_CY)` = c(22644, 22644, 
    22644, 22644, 22644, 22644)), row.names = c(NA, 6L), class = "data.frame")

使用此代码应用于我的数据:

Entropy= EntropyJoin%>% group_by(region_ID) %>%
           summarise(result=-sum(TOTPOP_CY*log((TOTPOP_CY/"sum(TOTPOP_CY)"),2)))

我收到一个错误:

Error: Problem with `summarise()` input `result`.
x non-numeric argument to binary operator
i Input `result` is `-sum(TOTPOP_CY * log((TOTPOP_CY/"sum(TOTPOP_CY)"), 2))`.
i The error occurred in group 1: region_ID = 0.
Run `rlang::last_error()` to see where the error occurred.

标签: rentropy

解决方案


您没有在上一步中命名列。你可能有类似的东西:

.... %>% mutate(sum(TOTPOP_CY))

您应该改为将其更正为:

.... %>% mutate(sum_TOTPOP = sum(TOTPOP_CY))

然后你可以使用:

Entropy = EntropyJoin%>% 
           group_by(region_ID) %>%
           summarise(result=-sum(TOTPOP_CY*log((TOTPOP_CY/sum_TOTPOP),2)))

尽管如此,如果您不想更改之前的步骤,您可以在此处使用反引号 (`) 引用变量名。

EntropyJoin %>% 
  group_by(region_ID) %>%
  summarise(result=-sum(TOTPOP_CY*log((TOTPOP_CY/`sum(TOTPOP_CY)`),2)))

推荐阅读