r - 当我尝试应用熵计算时,如何解决二进制运算符错误的非数字参数?
问题描述
当我尝试对我的数据应用熵计算时出现错误。
这是一个使用示例数据的示例:
#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.
解决方案
您没有在上一步中命名列。你可能有类似的东西:
.... %>% 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)))
推荐阅读
- typescript - AmCharts 4 中的可滚动外部图表图例
- mysql - 从日期中提取年份并插入新字段
- python - 我们可以使用 Keras 进行视频数据增强吗?
- python - 通过 pandas 绘图界面绘制带有第二个 y 轴的 pandas 数据框时设置图例位置
- quasar-framework - Quasar Framework:在 QLayoutHeader/QToolbar 中隐藏 QToolbarTitle
- pimcore - Pimcore CSV 从文件夹位置上传并自动创建数据对象
- java - 如何按照提交的顺序通过 websocket 代理在 Spring Boot 中发送消息?
- sql-server - Kafka Connect with MSSQL 不适用于删除操作
- docker - Docker:如何将容器内的 IP 重定向到另一个 IP
- angular - 禁用角度中的默认键值管道排序