https://mirrors.ustc.edu.cn/CRAN/
https://rstudio.com/products/rstudio/download/
安装包
install.packages('car') #建议在线安装,不建议本地安装
加载包
library(car)
car::vif()#单独加载包内的某个函数
更新包
update.packages() #更新所有包
update.packages('car') #更新指定包,以包名称作为函数参数即可
移除包
remove.packages('car')
#获取某个函数的帮助
?library
help('library')
#获取某个关键字的帮助
??library
help.search('library')
#获取某个package的帮助
help(package='ggplot2')
#获取当前工作路径
getwd()
#设置当前工作路径
setwd()
#获取文件路径
file.choose()
read.csv(file.choose())
产生变量对象
data1<-c(1:7)
data2<-c(3:9)
data3<-c(100:200)
保存工作空间save.image('20220827')
加载R文件load('20220827.RData')load(file.choose())
R的内置数据集一共有两种:R内部datasets包中的数据集以及安装的其他package中包含的数据集
data() #查看datasets包内置数据集
help(package='datasets') #查看datasets包帮助信息
#加载state.name数据集(美国50个州的名字)
> state.name
[1] "Alabama" "Alaska" "Arizona" "Arkansas"
[5] "California" "Colorado" "Connecticut" "Delaware"
[9] "Florida" "Georgia" "Hawaii" "Idaho"
[13] "Illinois" "Indiana" "Iowa" "Kansas"
[17] "Kentucky" "Louisiana" "Maine" "Maryland"
[21] "Massachusetts" "Michigan" "Minnesota" "Mississippi"
[25] "Missouri" "Montana" "Nebraska" "Nevada"
[29] "New Hampshire" "New Jersey" "New Mexico" "New York"
[33] "North Carolina" "North Dakota" "Ohio" "Oklahoma"
[37] "Oregon" "Pennsylvania" "Rhode Island" "South Carolina"
[41] "South Dakota" "Tennessee" "Texas" "Utah"
[45] "Vermont" "Virginia" "Washington" "West Virginia"
[49] "Wisconsin" "Wyoming"
#加载北美141条河流长度
> rivers
[1] 735 320 325 392 524 450 1459 135 465 600 330 336 280 315 870
[16] 906 202 329 290 1000 600 505 1450 840 1243 890 350 407 286 280
[31] 525 720 390 250 327 230 265 850 210 630 260 230 360 730 600
[46] 306 390 420 291 710 340 217 281 352 259 250 470 680 570 350
[61] 300 560 900 625 332 2348 1171 3710 2315 2533 780 280 410 460 260
[76] 255 431 350 760 618 338 981 1306 500 696 605 250 411 1054 735
[91] 233 435 490 310 460 383 375 1270 545 445 1885 380 300 380 377
[106] 425 276 210 800 420 350 360 538 1100 1205 314 237 610 360 540
[121] 1038 424 310 300 444 301 268 620 215 652 900 525 246 360 529
[136] 500 720 270 430 671 1770
#rivers对象重新赋值
> rivers<-c(2:4)
#赋值后改变原有含义
> rivers
[1] 2 3 4
#加载rivers数据集rivers
data(rivers)
#显示当前环境中的对象
> ls()
[1] "data0" "i" "lm1" "model.aic" "model.bic" "o"
[7] "rivers" "ss"
#移除某个对象
> rm(i)
#移除所有对象
> rm(list = ls())