R语言统计入门第一章基础知识——1.1初始步骤

  • Post author:
  • Post category:其他


#1.1初始步骤

install.packages("ISwR")#安装包
library(ISwR)#借阅包
plot(rnorm(1000))



#1.1.1 大型计算器

2+2
## [1] 4
exp(-2)
## [1] 0.1353353
rnorm(15)
##  [1] -0.09971231  0.76745740 -0.88572455 -1.23293365  0.92343985
##  [6]  0.57028888  0.56232673 -1.64383654 -1.24220628  0.10673117
## [11]  0.38478635  0.18986908  0.45876877  0.42285381 -0.83338142

#1.1.2 赋值

x<-2
x
## [1] 2
x+x
## [1] 4

#1.1.3 向量运算

weight<-c(60,72,57,90,95,72)
weight
## [1] 60 72 57 90 95 72
height<-c(1.75,1.80,1.65,1.90,1.74,1.91)
bmi<-weight/height^2
bmi
## [1] 19.59184 22.22222 20.93664 24.93075 31.37799 19.73630
sum(weight)
## [1] 446
sum(weight)/length(weight)
## [1] 74.33333
xbar<-sum(weight)/length(weight)
weight-xbar
## [1] -14.333333  -2.333333 -17.333333  15.666667  20.666667  -2.333333
(weight-xbar)^2
## [1] 205.444444   5.444444 300.444444 245.444444 427.111111   5.444444
sum(weight-xbar)^2
## [1] 8.077936e-28
sqrt(sum(weight-xbar)^2/(length(weight)-1))
## [1] 1.271057e-14
mean(weight)
## [1] 74.33333
sd(weight)
## [1] 15.42293

#1.1.4 标准过程

t.test(bmi,mu=22.5)#t检验
## 
## 	One Sample t-test
## 
## data:  bmi
## t = 0.34488, df = 5, p-value = 0.7442
## alternative hypothesis: true mean is not equal to 22.5
## 95 percent confidence interval:
##  18.41734 27.84791
## sample estimates:
## mean of x 
##  23.13262

#1.1.5 作图

plot(height,weight)#绘制散点图,height为x,weight为y

在这里插入图片描述

plot(height,weight,pch=2)#pch为绘图符号
hh<-seq(1.65,1.90,0.05)#同等与c(1.65,1.70,1.75,1.80,1.85,1.90),seq()建立数字等差序列
lines(hh,22.5*hh^2)#将直线x=hh,y=22.5*hh^2,增加到图中

在这里插入图片描述



版权声明:本文为qq_38742877原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。