简体   繁体   English

参数不是数字

[英]Argument is not numeric

I would like to visualize the number of people infected with COVID-19, but I am unable to obtain the mortality rate because the number of deaths is derived by int when obtaining the mortality rate per 100,000 population for each prefecture.我想可视化感染 COVID-19 的人数,但我无法获得死亡率,因为在获得每个县的每 100,000 人口的死亡率时,死亡人数是由 int 得出的。

What I want to achieve我想要达到的目标

I want to find the solution of "covid19j_20200613$POP2019 * 100" by setting the data type of "covid19j_20200613$deaths" to num.我想通过将“covid19j_20200613$deaths”的数据类型设置为num来找到“covid19j_20200613$POP2019 * 100”的解决方案。

Error message.错误信息。

 Error in covid19j_20200613$deaths/covid19j_20200613$POP2019: 
   Argument of binary operator is not numeric 

Source code in question.有问题的源代码。

library(spdep)
library(sf)
library(spatstat)
library(tidyverse)
library(ggplot2)

needs::prioritize(magrittr)

covid19j <- read.csv("https://raw.githubusercontent.com/kaz-ogiwara/covid19/master/data/prefectures.csv",
                     header=TRUE)

# Below is an example for May 20, 2020.
# Month and date may be changed

covid19j_20200613 <- dplyr::filter(covid19j,
                                   year==2020,
                                   month==6,
                                   date==13)
covid19j_20200613$CODE <- 1:47

covid19j_20200613[is.na(covid19j_20200613)] <- 0

pop19 <- read.csv("/Users/carlobroschi_imac/Documents/lectures/EGDS/07/covid19_data/covid19_data/pop2019.csv", header=TRUE)

covid19j_20200613 <- dplyr::inner_join(covid19j_20200613, pop19, 
                                       by = c("CODE" = "CODE"))

# Load Japan prefecture administrative boundary data
jpn_pref <- sf::st_read("/Users/carlobroschi_imac/Documents/lectures/EGDS/07/covid19_data/covid19_data/jpn_pref.shp")
# Data and concatenation
jpn_pref_cov19 <- dplyr::inner_join(jpn_pref, covid19j_20200613, by=c("PREF_CODE"="CODE"))

ggplot2::ggplot(data = jpn_pref_cov19) + 
  geom_sf(aes(fill=testedPositive)) + 
  scale_fill_distiller(palette="RdYlGn") + 
  theme_bw() +
  labs(title = "Tested Positiv of Covid19 (2020/06/13)")


# Mortality rate per 100,000 population
# Population number in units of 1000
as.numeric(covid19j_20200613$deaths)
covid19j_20200613$deaths_rate <- covid19j_20200613$deaths / covid19j_20200613$POP2019 * 100

Source code in question.有问题的源代码。

prefectures.csv
https://docs.google.com/spreadsheets/d/11C2vVo-jdRJoFEP4vAGxgy_AEq7pUrlre-i-zQVYDd4/edit?usp=sharing
pop2019.csv
https://docs.google.com/spreadsheets/d/1CbEX7BADutUPUQijM0wuKUZFq2UUt-jlWVQ1ipzs348/edit?usp=sharing

What we tried我们尝试了什么

I tried to put "as.numeric(covid19j_20200613$deaths)" before the calculation and set the number of dead to type num, but I got the same error message during the calculation.我尝试在计算之前输入“as.numeric(covid19j_20200613$deaths)”并将死亡人数设置为 num,但在计算过程中我收到了相同的错误消息。

Additional information (FW/tool versions, etc.)附加信息(固件/工具版本等)

iMac M1 2021, R 4.2.0 iMac M1 2021,R 4.2.0

Translated with www.DeepL.com/Translator (free version)使用www.DeepL.com/Translator翻译(免费版)

as.numeric() does not permanently change the data type - it only does it temporarily. as.numeric()不会永久更改数据类型 - 它只是临时更改。

So when you're running as.numeric(covid19j_20200613$deaths) , this shows you the column deaths as numeric, but the column will stay a character.因此,当您运行as.numeric(covid19j_20200613$deaths)时,这会将deaths列显示为数字,但该列将保留一个字符。

So if you want to coerce the data type, you need to also reassign:所以如果你想强制数据类型,你还需要重新分配:

covid19j_20200613$deaths <- as.numeric(covid19j_20200613$deaths)
covid19j_20200613$POP2019 <- as.numeric(covid19j_20200613$POP2019)

# Now you can do calculations
covid19j_20200613$deaths_rate <- covid19j_20200613$deaths / covid19j_20200613$POP2019 * 100

It's easier to read if you use mutate from dplyr :如果您使用dplyrmutate ,则更容易阅读:

covid19j_20200613 <- covid19j_20200613 |>
  mutate(
    deaths = as.numeric(deaths),
    POP2019 = as.numeric(POP2019),
    death_rate = deaths / POP2019 * 100
  )

Result结果

  deaths POP2019 deaths_rate
1     91    5250  1.73333333
2      1    1246  0.08025682
3      0    1227  0.00000000
4      1    2306  0.04336513
5      0     966  0.00000000

PS: your question is really difficult to follow! PS:你的问题真的很难理解! There is a lot of stuff that we don't actually need to answer it, so that makes it harder for us to identify where the issue is.有很多东西我们实际上不需要回答,所以这让我们更难确定问题出在哪里。 For example, all the data import, the join, the ggplot...例如,所有的数据导入、连接、ggplot...

When writing a question, please only include the minimal elements that lead to a problem.写问题时,请仅包含导致问题的最小元素。 In your case, we only needed a sample dataset with the deaths and POP2019 columns, and the two lines of code that you tried to fix at the end.在您的情况下,我们只需要一个包含deathsPOP2019列的示例数据集,以及您最后尝试修复的两行代码。

If you look at str(covid19j) you'll see that the deaths column is a character column containing a lot of blanks.如果您查看str(covid19j) ,您会看到deaths列是一个包含大量空格的字符列。 You need to figure out the structure of that column to read it properly.您需要弄清楚该列的结构才能正确阅读它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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