修改r语言month变为NA的错误:#install.packages("tidyverse") #install.packages("ggplot2") library(scales) library(ggplot2) library(dplyr) library(tidyverse) setwd("C:/Users/25341/Desktop") data1 <- read.csv("covid19.csv") str(data1) year<-strsplit(data1$Date,split = "-") year1<-do.call(rbind, year)[,1] data1$year1<-year1 data1<- mutate(data1, year =substr(year1,1,4)) result1<-aggregate(data1$New_cases, data1[,c("year","Country","Country_code")],sum) result2<-aggregate(data1$New_deaths, data1[,c("year","Country","Country_code")],sum) result<-cbind(result1,result2$x) filter(data1,data1$Country=="China" | data1$Country=="American Samoa" | data1$Country=="British Virgin Islands" | data1$Country=="India" | data1$Country=="Japan" ) filter(result,result$Country=="China" | result$Country=="American Samoa" | result$Country=="British Virgin Islands" | result$Country=="India" | result$Country=="Japan" ) data2<-subset(data1,data1$Country=="China" | data1$Country=="American Samoa" | data1$Country=="British Virgin Islands" | data1$Country=="India" | data1$Country=="Japan" ) df<-filter(result,result$Country=="China" | result$Country=="American Samoa" | result$Country=="British Virgin Islands" | result$Country=="India" | result$Country=="Japan" ) names(df)<-c("year","Country", "Country_code","newcases" ,"newdeaths") df$newcases=as.numeric(df$newcases) str(df) p1.1<-ggplot(df,aes(x=year,y=newcases,group = Country,color=Country,shape=Country,linetype=Country))+ geom_line(size=1)+ geom_point(size=2) p1.1 p1.2<-ggplot(df,aes(x=year,y=newdeaths,group = Country,color=Country,shape=Country,linetype=Country))+ geom_line(size=1)+ geom_point(size=2) p1.2 month<-strsplit(data1$Date,split = "-") month1<-do.call(rbind,month)[,1] data1$month1<-month1 data2<- mutate(data1, month =substr(year1,5,7)) result1.1<-aggregate(data2$New_cases, data2[,c("year","month","Country","Country_code")],sum) result1.1 result2.1<-aggregate(data2$New_deaths, data2[,c("year","month","Country","Country_code")],sum) result2.1 result11<-cbind(result1.1,result2.1$x) df1<-filter(result11,result11$Country=="China" | result11$Country=="American Samoa" | result11$Country=="British Virgin Islands" | result11$Country=="India" | result11$Country=="Japan" ) names(df1)<-c("year","month","Country", "Country_code","newcases" ,"newdeaths") df1$month <- factor(df1$month ,levels = c("/1/", "/2/", "/3/", "/4/","/5/", "/6/", "/7/", "/8/","/9/","/10", "/11", "/12")) df1
时间: 2023-07-06 15:31:58 浏览: 88
根据你提供的代码,修改R语言month变为NA的错误的方法如下:
1. 将month1中的月份提取出来,而不是年份:
将以下代码:
```
month1<-do.call(rbind,month)[,1]
```
修改为:
```
month1<-do.call(rbind,month)[,2]
```
2. 将month1中的月份格式转换为数字:
在以下代码后面添加一行:
```
month1<-as.numeric(month1)
```
3. 找到month1中缺失值的位置并将其替换成NA:
在以下代码后面添加两行:
```
missing_month<-which(month1=="")
month1[missing_month]<-NA
```
4. 修改df1中month的列名为month1:
将以下代码:
```
names(df1)<-c("year","month","Country", "Country_code","newcases" ,"newdeaths")
```
修改为:
```
names(df1)<-c("year","month1","Country", "Country_code","newcases" ,"newdeaths")
```
5. 最后,重新运行代码并确认month1中没有缺失值。
阅读全文