aggregate(pat1[,"年龄"],by=pat1["性别"],mean)
aggregate(pat1[,"年龄"],by=pat1["性别"],min)
aggregate(pat1[,"年龄"],by=pat1["性别"],max)
m<-diag(c(1,2,3,4,5,6,7,8,9,10))
m2<-m*2
m3<-m*3
m1<-cbind(m,m2,m3)
arr <- array(m1,dim=c(10,10,2))
m<-diag(c(1,2,3,4,5,6,7,8,9,10))
m2<-m*2
m3<-m*3
m1<-cbind(m,m2,m3)
arr <- array(m1,dim=c(10,10,2))
print(arr)
m<-diag(c(1,2,3,4,5,6,7,8,9,10))
m2<-m*2
m3<-m*3
m1<-cbind(m,m2,m3)
arr <- array(m1,dim=c(10,10,3))
print(arr)
```{r}
pat[pat[,"出院时间"]-pat[,"入院时间"]<0,"入院时间"]<-20200205
pat[pat[,"吸烟"]=="有"&as.numeric(pat[,"年龄"])>40&pat[,"出院时间"]-pat[,"入院时间"]>10,c("分型","年龄")]
```
```{r}
pat[pat[,"出院时间"]-pat[,"入院时间"]<0,"入院时间"]<-20200205
pat[pat[,"吸烟"]=="有"&as.numeric(pat[,"年龄"])>40&pat[,"出院时间"]-pat[,"入院时间"]>10,c("分型","年龄")]
```
1. 把covid19.csv读入到数据框`pat1`中，输出其列名和前三行的内容；
```{r}
setwd("C:/Users/DELL/Desktop/大三上/实用统计软件/作业/2")
pat1<-read.csv("covid19.csv",header=TRUE,na.strings="")
head(pat1,3)
```
2. 找出性别中有缺失值的行并将其去掉，去掉后的数据框仍然记为`pat1`；
```{r}
row.na<-which(is.na(pat1$性别))
pat1<-pat1[-row.na,]
pat1
```
3. 分别按性别、入院日期汇总数据并输出不同性别的入院人数、不同入院日期的入院人数；
```{r}
table(pat1$性别)
table(pat1$入院时间)
```
4. 分性别计算平均年龄、最大年龄和最小年龄；
```{r}
aggregate(pat1[,"年龄"],by=pat1["性别"],mean)
aggregate(pat1[,"年龄"],by=pat1["性别"],max)
aggregate(pat1[,"年龄"],by=pat1["性别"],min)
```
5. 把年龄分成0—10, 11—20, ……, 70以上各段， 保存为“年龄段”变量，并将其加入到数据框`pat1`中；
```{r}
pat1[which(pat1$年龄>=0&pat1$年龄<=10),"年龄段"]<-"0-10"
pat1[which(pat1$年龄>=11&pat1$年龄<=20),"年龄段"]<-"11-20"
pat1[which(pat1$年龄>=21&pat1$年龄<=30),"年龄段"]<-"21-30"
pat1[which(pat1$年龄>=31&pat1$年龄<=40),"年龄段"]<-"31-40"
pat1[which(pat1$年龄>=41&pat1$年龄<=50),"年龄段"]<-"41-50"
pat1[which(pat1$年龄>=51&pat1$年龄<=60),"年龄段"]<-"51-60"
pat1[which(pat1$年龄>=61&pat1$年龄<=70),"年龄段"]<-"61-70"
pat1[which(pat1$年龄>70),"年龄段"]<-"70以上"
```
6. 用年龄段和性别交叉汇总发病人数和百分比(结果乘以100并保留一位小数)， 保存到“年龄性别分布.csv”中 要求将每个年龄段的男性病人数、女性发病人数存为一行。
```{r}
num<-table(pat1[,"年龄段"],pat1[,"性别"],useNA="ifany")
num
total<-nrow(pat1)
pervec<-paste0(round(num*100/total,1),"%")
permat<-matrix(pervec,nrow=8,ncol=2)
per<-data.frame(permat,row.names=rownames(num))
colnames(per)<-colnames(num)
per
write.csv(num,file="年龄性别分布.csv",fileEncoding = "UTF-8")
```
knitr::opts_chunk$set(echo = TRUE)
pat[pat[,"出院时间"]-pat[,"入院时间"]<0,"入院时间"]<-20200205
pat[pat[,"出院时间"]-pat[,"入院时间"]<0,"入院时间"]<-20200205
pat[(pat[,"出院时间"]-pat[,"入院时间"])<0,"入院时间"]<-20200205
pat[,"出院时间"]
pat[(pat[,"出院时间"]-pat[,"入院时间"])<0,"入院时间"]<-20200205
pat[,"出院时间"]
pat[,"出院时间"]
setwd("D:/homework_R/HW2")
pat1<-read.csv("covid19.csv",header=TRUE,na.strings="")
head(pat1,3)
setwd("D:/homework_R/HW2")
pat2<-read.csv("covid19-3.csv",header=TRUE,na.strings='')
head(pat2,3)
pat[,"出院时间"]
pat<-merge(pat1,pat2,by.x="序号",by.y="序号",all.x=TRUE)
pat[,"出院时间"]
pat[(pat[,"出院时间"]-pat[,"入院时间"])<0,"入院时间"]<-20200205
pat
pat[pat[,"吸烟"]=="有"&as.numeric(pat[,"年龄"])>40&pat[,"出院时间"]-pat[,"入院时间"]>10,c("分型","年龄")]
row.na<-which(is.na(pat1$性别))
pat1<-pat1[-row.na,]
pat1
knitr::opts_chunk$set(echo = TRUE)
#cumsum和cumprod计算累加和累乘积
cumsum(1:5)
cumprod(1:5)
#
x <- rep(0, n)
#cumsum和cumprod计算累加和累乘积
cumsum(1:5)
cumprod(1:5)
#
x <- rep(0, 2)
#cumsum和cumprod计算累加和累乘积
cumsum(1:5)
cumprod(1:5)
#rep()函数用来产生重复数值
x <- rep(0, 2);x
rep(c(1,3), c(3,4))
rep(c(1,3), each=2)
x<-seq(1:100)
sqrt(x)
x<-seq(1:100);sqrt(x);x^(1/3)
x<-seq(from=0,to=1,length=100)
x<-seq(from=0,to=1,length=100);x
x<-seq(from=0,to=1,length=101);x
knitr::opts_chunk$set(echo = TRUE)
d <- read.csv("covid19.csv", header = TRUE, na.strings = "",
row.names = "序号")
getwd()
getwd()
setwd("D:/practice_for_course/Week4")
d <- read.csv("covid19.csv", header = TRUE, na.strings = "",
row.names = "序号")
res <- table(d[,"性别"])
print(res)
barplot(res)
barplot(res, main="性别分布", col=c("brown2", "aquamarine1"))
barplot(res, main="性别分布", col=c("red", "blue"))
res <- with(d, table(性别, 分型)); res
barplot(res, legend = TRUE)
res <- with(d, table(性别, 分型)); res
barplot(res, legend = TRUE)
barplot(res, legend = FALSE)
setwd("D:/practice_for_course/Week4")
d <- read.csv("covid19.csv", header = TRUE, na.strings = "",
row.names = "序号")
res <- table(d[,"性别"])
print(res)
barplot(res)
barplot(res, main="性别分布", col=c("brown2", "aquamarine1"))
barplot(res, main="性别分布", col=c("red", "blue"))
barplot(res, main="性别分布", col=c("brown2", "aquamarine1"), horiz = TRUE)   #用 horiz = TRUE 指定横排的条形图
#列联表条形图
res <- with(d, table(性别, 分型)); res
barplot(res, legend = TRUE,col=c("red","blue"))     #legend输出行变量分类
barplot(res, legend = FALSE)
barplot(res, beside=TRUE, legend=TRUE)   #并排条形图
hist(d$年龄)
hist(d$年龄)    #等价于hist(d[, "年龄"])
hist(d[, "年龄"], freq = FALSE)   #y轴从计数变为比例
hist(d[, "年龄"], col = rainbow(15), main = '年龄', xlab = '年龄', ylab = '频数')
#直方图叠加
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "pink", main = '年龄',
xlab = '', ylab = '频数')
hist(d[!idx, "年龄"], freq = FALSE, col = rgb(0,0.5, 0.5, 0.5), add = TRUE)
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "pink", main = '年龄',
xlab = '', ylab = '频数')
hist(d[!idx, "年龄"], freq = FALSE, col = "blue", add = TRUE)
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "blue", main = '年龄',
xlab = '', ylab = '频数')
hist(d[!idx, "年龄"], freq = FALSE, col = "pink", add = TRUE)
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "pink", main = '年龄',
xlab = '', ylab = '频数')
hist(d[!idx, "年龄"], freq = FALSE, col = rgb(0,0.5, 0.5, 0.5), add = TRUE)
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "pink", main = '年龄',
xlab = '', ylab = '频数',xlim=c(0,100))
hist(d[!idx, "年龄"], freq = FALSE, col = rgb(0,0.5, 0.5, 0.5), add = TRUE)
hist(d[, "年龄"], freq = FALSE, col = rainbow(10), main = '年龄', xlab = '', ylab = '频数')
lines(density(d[, "年龄"]), lwd = 3, col = "black")
hist(d[, "年龄"], freq = FALSE, col = rainbow(12), main = '年龄', xlab = '', ylab = '频数')
lines(density(d[, "年龄"]), lwd = 5, col = "black")
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d)
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d,col="purple")
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d,col="purple2")
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d,col="purple0")
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d,col="purple1")
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d,col="purple10")
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d,col="purple7")
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d,col="purple3")
knitr::opts_chunk$set(echo = TRUE)
res <- with(d, table(性别, 分型)); res
barplot(res, legend = TRUE,col=c("red","blue"))     #legend输出行变量分类
barplot(res, legend = FALSE)
barplot(res, beside=TRUE, legend=TRUE)   #并排条形图
pairs(trees)
curve(1 - 3*x - x^2, -4, 2)
curve((1-x^4)^(1/4),0,1)
curve(1 - 3*x - x^2, -4, 2)
curve((1-x^4)^(1/4),0,2)
curve(1 - 3*x - x^2, -4, 2)
curve((1-x^4)^(1/4),-1,1)
plot(疗程 ~ 年龄, data = d, main = "疗程与年龄的关系")  #原始图表
plot(疗程 ~ 年龄, data = d, main = "疗程与年龄的关系",
pch= 16, col = "blue", cex = 2)
plot(疗程 ~ 年龄, main = "疗程与年龄的关系", data = d,
col=ifelse(性别=='男', 'blue', 'red'), pch=16) #用ifelse区别性别颜色
plot(疗程 ~ 年龄, main = "疗程与年龄的关系", data = d,
col=ifelse(性别=='男', 'blue', 'red'), pch=16,
cex = 1 + (入院时间- min(入院时间))/(max(入院时间)- min(入院时间)))
#散点图矩阵
pairs(trees)
plot(疗程 ~ 年龄, data = d, main = "疗程与年龄的关系")  #原始图表
plot(疗程 ~ 年龄, data = d, main = "疗程与年龄的关系",
pch= 5, col = "blue", cex = 1)
plot(疗程 ~ 年龄, main = "疗程与年龄的关系", data = d,
col=ifelse(性别=='男', 'blue', 'red'), pch=16) #用ifelse区别性别颜色
plot(疗程 ~ 年龄, main = "疗程与年龄的关系", data = d,
col=ifelse(性别=='男', 'blue', 'red'), pch=16,
cex = 1 + (入院时间- min(入院时间))/(max(入院时间)- min(入院时间)))
#散点图矩阵
pairs(trees)
x <- seq(-3, 3, length = 100)
y <- x
phi <- dnorm(x)
normal.mat <- phi %o% phi
x <- seq(-3, 3, length = 100)
y <- x
phi <- dnorm(x)
normal.mat <- phi %o% phi
persp(normal.mat)
x <- seq(-3, 3, length = 100)
y <- x
phi <- dnorm(x)
normal.mat <- phi %o% phi
persp(normal.mat,col="blue")
x <- seq(-3, 3, length = 100)
y <- x
phi <- dnorm(x)
normal.mat <- phi %o% phi
persp(normal.mat,col="blue")
contour(x, y, normal.mat)
image(x, y, normal.mat, col=terrain.colors(20))
contour(x, y, normal.mat, add = TRUE)
x <- seq(0, 2*pi, length=200); y <- sin(x)
plot(x, y, type='l')
points((0:4)*pi/2, sin((0:4)*pi/2), col = "red", pch = 16, cex = 2)
#VS
x <- seq(0, 2*pi, length=20); y <- sin(x)
plot(x, y, type='b')
x <- seq(0, 2*pi, length=200);
plot(x, sin(x), type='l', col = "red")
lines(x, cos(x), col= "blue")
lines(x, sin(x/2), col="green", lty=2)
abline(h = 0, col = "gray")
legend(0, -0.7, col=c("red", "blue"), lty=c(1,1), lwd=c(2,2), legend=c("sin", "cos"))
legend("topleft", col=c("red", "green"), lty=c(1,2),lwd=c(2,2), legend=c("sin(x)", "sin(x/2)"))
x <- table(as.Date(as.character(d[,"入院时间"]), format = "%Y%m%d"))
plot(x, type="b", main = "入院人数（按天计）", ylab ="入院人数", axes = FALSE) # ax
axis(2); axis(1, at = seq(along = x), labels = substr(names(x),7,10), las=2) # la
plot(x, type="b", main = "入院人数（按天计）", ylab ="入院人数", axes = FALSE) # ax
axis(2); axis(1, at = seq(along = x), labels = substr(names(x),6,10))
text(which.max(x)-2, max(x), "最高峰") # 前两个参数是坐标位置，第三个参数 `lab
mtext(paste("边缘", 1:4), side = 1:4, line = 2)
plot(x, type="b", main = "入院人数（按天计）", ylab ="入院人数", axes = FALSE) # ax
axis(2); axis(1, at = seq(along = x), labels = substr(names(x),6,10))
text(which.max(x)-3, max(x), "最高峰") # 前两个参数是坐标位置，第三个参数 `lab
mtext(paste("边缘", 1:4), side = 1:4, line = 2)
library(showtext)
show::showtext.begin()
library(showtext)
download.packages(showtext)
install.packages(showtext)
install.packages("showtext")
library(showtext)
show::showtext.begin()
install.packages("show")
library(showtext)
show::showtext.begin()
av <- available.packages(filters=list())
av[av[, "show"] == pkg , ]
av <- available.packages(filters=list())
av[av[, "包"] == "show" , ]
av <- available.packages(filters=list())
av[av[, "包"] == show , ]
av <- available.packages(filters=list())
av[av[, "Package"] == show , ]
av <- available.packages(filters=list())
av[av[, "Package"] == "show" , ]
install.packages("show")
library(showtext)
showtext::showtext.begin()
showtext::showtext_begin()
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "blue", main = '年龄', xlab = '', ylab = '频数')
hist(d[!idx, "年龄"], freq = FALSE, col = "pink", add = FALSE)
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "pink", main = '年龄', xlab = '', ylab = '频数',xlim=c(range(d$年龄)))
hist(d[!idx, "年龄"], freq = FALSE, col = rgb(0,0.5, 0.5, 0.5), add = TRUE)
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "pink", main = '年龄', xlab = '', ylab = '频数',xlim=c(min(d$年龄),max(d$年龄)))
hist(d[!idx, "年龄"], freq = FALSE, col = rgb(0,0.5, 0.5, 0.5), add = TRUE)
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "pink", main = '年龄', xlab = '', ylab = '频数',xlim=c(0,max(d$年龄)))
hist(d[!idx, "年龄"], freq = FALSE, col = rgb(0,0.5, 0.5, 0.5), add = TRUE)
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "pink", main = '年龄', xlab = '', ylab = '频数',xlim=c(0,max(d$年龄)))
hist(d[!idx, "年龄"], freq = FALSE, col = rgb(0,0.5, 0.5, 0.5), add = TRUE,xlim=c(0,max(d$年龄)))
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "pink", main = '年龄', xlab = '', ylab = '频数',xlim=c(0,100))
hist(d[!idx, "年龄"], freq = FALSE, col = rgb(0,0.5, 0.5, 0.5), add = TRUE)
idx <- d$性别=="女"
hist(d[idx, "年龄"], freq = FALSE, col = "pink", main = '年龄', xlab = '', ylab = '频数',xlim=c(0,(max(d$年龄)%/%10+1)*10))
hist(d[!idx, "年龄"], freq = FALSE, col = rgb(0,0.5, 0.5, 0.5), add = TRUE)
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d,col="purple1")
boxplot(d$年龄~d$性别)
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d,col="purple1")
boxplot(年龄~性别)
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d,col="purple1")
boxplot(d$年龄 ~ d$性别)
seq(1,10,length=10)
seq(0,1,length=10)
seq(1,length=10)
seq(1,11,length=10)
x <- table(as.Date(as.character(d[,"入院时间"]), format = "%Y%m%d"))
plot(x, type="b", main = "入院人数（按天计）", ylab ="入院人数", axes = FALSE) # axes 暂时禁止坐标轴生成，从而添加自定义坐标轴
axis(2); axis(1, labels = substr(names(x),7,10), las=2) #刻度线标签平行（=0）还是垂直（=2）于坐标轴
x <- table(as.Date(as.character(d[,"入院时间"]), format = "%Y%m%d"))
plot(x, type="b", main = "入院人数（按天计）", ylab ="入院人数", axes = FALSE) # axes 暂时禁止坐标轴生成，从而添加自定义坐标轴
axis(2); axis(1, at=seq(x),labels = substr(names(x),7,10), las=2) #刻度线标签平行（=0）还是垂直（=2）于坐标轴
x <- table(as.Date(as.character(d[,"入院时间"]), format = "%Y%m%d"))
plot(x, type="b", main = "入院人数（按天计）", ylab ="入院人数", axes = FALSE) # axes 暂时禁止坐标轴生成，从而添加自定义坐标轴
axis(2); axis(1, at = seq(along = x), labels = substr(names(x),7,10), las=2) #刻度线标签平行（=0）还是垂直（=2）于坐标轴
x <- table(as.Date(as.character(d[,"入院时间"]), format = "%Y%m%d"))
plot(x, type="b", main = "入院人数（按天计）", ylab ="入院人数", axes = FALSE) # axes 暂时禁止坐标轴生成，从而添加自定义坐标轴
axis(2); axis(1, at=seq(x),labels = substr(names(x),7,10), las=2) #刻度线标签平行（=0）还是垂直（=2）于坐标轴
#axis()
x <- table(as.Date(as.character(d[,"入院时间"]), format = "%Y%m%d"))
plot(x, type="b", main = "入院人数（按天计）", ylab ="入院人数", axes = FALSE) # axes 暂时禁止坐标轴生成，从而添加自定义坐标轴
axis(2); axis(1, at = seq(along = x), labels = substr(names(x),7,10), las=2) #刻度线标签平行（=0）还是垂直（=2）于坐标轴
seq(1,20,length=10)
x <- table(as.Date(as.character(d[,"入院时间"]), format = "%Y%m%d"))
plot(x, type="b", main = "入院人数（按天计）", ylab ="入院人数", axes = FALSE) # axes 暂时禁止坐标轴生成，从而添加自定义坐标轴
axis(2); axis(1, at = seq(along = x), labels = substr(names(x),7,10), las=2) #刻度线标签平行（=0）还是垂直（=2）于坐标轴
layout(matrix(c(1,1,2,3), 2,2, byrow = TRUE))
install.packages("ggplot2")
knitr::opts_chunk$set(echo = TRUE)
setwd("D:/homework_R/HW2")
#第一题
df1 <- read.csv("covid19.csv",header=TRUE,fileEncoding="GBK")
head(df1,n=5)
#第二题
df2 <- read.csv("covid19-2.csv",header=TRUE,fileEncoding="GBK")
tail(df2,n=3)
#第三题
df3 <- read.csv("covid19-3.csv",header=TRUE,fileEncoding="GBK")
tail(df3,n=3)
#第四题
df4<- rbind(df2,df3)
df4<-df4[!duplicated(df4),]
dim(df4)
df4
#第五题
df<- merge(df1,df4,by="序号",all.x=TRUE)
dim(df)
#第六题
df[is.na(df$疗程),]
df_copy<-df
df_copy[,"入院时间"] <- as.Date(as.character(df_copy[,"入院时间"]), format = "%Y%m%d")
df_copy[,"出院时间"] <- as.Date(as.character(df_copy[,"出院时间"]), format = "%Y%m%d")
df_copy
df[is.na(df$疗程),"疗程"]<- as.numeric(df_copy[is.na(df_copy$疗程),"出院时间"]-df_copy[is.na(df_copy$疗程),"入院时间"])
hist(df[, "疗程"],  main = 'Frequency distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency')
hist(df[, "疗程"],  main = 'Frequency distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency',frq=FALSE)
lines(density(df[, "疗程"]), lwd = 5, col = "black"))
hist(df[, "疗程"],  main = 'Frequency distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency')
hist(df[, "疗程"],  main = 'Frequency distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency',frq=FALSE)
lines(density(df[, "疗程"]), lwd = 5, col = "black")
hist(df[, "疗程"],  main = 'Frequency distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency')
hist(df[, "疗程"],  main = 'Frequency distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency',freq=FALSE)
lines(density(df[, "疗程"]), lwd = 5, col = "black")
hist(df[, "疗程"],  main = 'Frequency(number) distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency')
hist(df[, "疗程"],  main = 'Frequency(proportion) distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency',freq=FALSE)
lines(density(df[, "疗程"]), lwd = 5, col = "black")
res1<-table(d$性别,d$分型)
res1<-table(d$性别,d$分型);res1
res1<-table(d$性别,d$分型);res1
barplot(res1,main="Sex distribution of patients with different disease types",besides=TRUE,col=c("cyan","darkgoldenrod"),legend=TRUE)
res1<-table(d$性别,d$分型);res1
barplot(res1,main="Sex distribution of patients with different disease types",beside=TRUE,col=c("cyan","darkgoldenrod"),legend=TRUE)
res1<-table(d$性别,d$分型);res1
barplot(res1,main="Sex distribution of patients with different disease types",beside=TRUE,col=c("cyan","darkgoldenrod"),legend=TRUE)
barplot(年龄~性别,col=c("cyan","darkgoldenrod"))
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"))
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df)
df[is.na(df$性别),"性别"] <- ""
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df)
df[is.na(df$性别),"性别"] <- ""
df
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df)
df[is.na(df$性别),"性别"] <- NULL
df
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df)
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df,useNA="no")
df[df$性别=="",性别]<- NA
df[df$性别=="""",性别]<- NA
df[,性别==""]<- NA
df[,df$性别==""]<- NA
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df)
df[,df$性别==""]<- NA
df
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df)
df[df$性别=="",]<- NA
df
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df)
View(df)
View(df)
showtext::showtext_begin()   #不加这句话也可以
library(showtext)
library(showtextdb)
library(sysfonts)
library(showtext)
showtext::showtext_begin()   #不加这句话也可以
#showtext::showtext_begin()   #不加这句话也可以
boxplot(d$年龄)
boxplot(年龄 ~ 性别, data = d,col="purple1")
boxplot(d$年龄 ~ d$性别)
df[df$性别=="",]<- NA
df[df$性别=="","性别"]<- NA
df[df$性别=="","性别"]<- as.NA(0)
df[df$性别=="","性别"]<- NA
df[df$性别==NA,"性别"]<- NA
idx.na <- apply(is.na(df), 1, any)
df[idx.na, ]
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df)
setwd("D:/homework_R/HW2")
#第一题
df1 <- read.csv("covid19.csv",header=TRUE,fileEncoding="GBK",na.string="")
head(df1,n=5)
#第二题
df2 <- read.csv("covid19-2.csv",header=TRUE,fileEncoding="GBK")
tail(df2,n=3)
#第三题
df3 <- read.csv("covid19-3.csv",header=TRUE,fileEncoding="GBK")
tail(df3,n=3)
#第四题
df4<- rbind(df2,df3)
df4<-df4[!duplicated(df4),]
dim(df4)
df4
#第五题
df<- merge(df1,df4,by="序号",all.x=TRUE)
dim(df)
#第六题
df[is.na(df$疗程),]
df_copy<-df
df_copy[,"入院时间"] <- as.Date(as.character(df_copy[,"入院时间"]), format = "%Y%m%d")
df_copy[,"出院时间"] <- as.Date(as.character(df_copy[,"出院时间"]), format = "%Y%m%d")
df[is.na(df$疗程),"疗程"]<- as.numeric(df_copy[is.na(df_copy$疗程),"出院时间"]-df_copy[is.na(df_copy$疗程),"入院时间"])
hist(df[, "疗程"],  main = 'Frequency(number) distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency(number)')
hist(df[, "疗程"],  main = 'Frequency(proportion) distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency(proportion)',freq=FALSE)
lines(density(df[, "疗程"]), lwd = 5, col = "black")
res1<-table(d$性别,d$分型);res1
res1<-table(df$性别,df$分型);res1
barplot(res1,main="Sex distribution of patients with different disease types",beside=TRUE,col=c("cyan","darkgoldenrod"),legend=TRUE)
idx.na <- apply(is.na(df), 1, any)
df[idx.na, ]
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df)
#idx.na <- apply(is.na(df), 1, any)
#df[idx.na, ]
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df)
setwd("D:/homework_R/HW2")
#第一题
df1 <- read.csv("covid19.csv",header=TRUE,fileEncoding="GBK",na.string="")
head(df1,n=5)
#第二题
df2 <- read.csv("covid19-2.csv",header=TRUE,fileEncoding="GBK")
tail(df2,n=3)
#第三题
df3 <- read.csv("covid19-3.csv",header=TRUE,fileEncoding="GBK")
tail(df3,n=3)
#第四题
df4<- rbind(df2,df3)
df4<-df4[!duplicated(df4),]
dim(df4)
df4
#第五题
df<- merge(df1,df4,by="序号",all.x=TRUE)
dim(df)
#第六题
df[is.na(df$疗程),]
df_copy<-df
df_copy[,"入院时间"] <- as.Date(as.character(df_copy[,"入院时间"]), format = "%Y%m%d")
df_copy[,"出院时间"] <- as.Date(as.character(df_copy[,"出院时间"]), format = "%Y%m%d")
df[is.na(df$疗程),"疗程"]<- as.numeric(df_copy[is.na(df_copy$疗程),"出院时间"]-df_copy[is.na(df_copy$疗程),"入院时间"])
hist(df[, "疗程"],  main = 'Frequency(number) distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency(number)')
hist(df[, "疗程"],  main = 'Frequency(proportion) distribution histogram of treatment', xlab = 'Treatment', ylab = 'Frequency(proportion)',freq=FALSE)
lines(density(df[, "疗程"]), lwd = 5, col = "black")
#idx.na <- apply(is.na(df), 1, any)
#df[idx.na, ]
boxplot(年龄~性别,col=c("cyan","darkgoldenrod"),data=df)
