[关闭]
@fanxy 2021-09-08T08:14:15.000000Z 字数 2469 阅读 3178

第一讲 R语言基础

樊潇彦 复旦大学经济学院 数量软件

1. R语言介绍

  1. # P8 赋值、统计与作图
  2. age <- c(1,3,5,2,11,9,3,9,12,3)
  3. weight <- c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
  4. mean(weight)
  5. sd(weight)
  6. cor(age,weight)
  7. plot(age,weight)

image_1cn8gnkqcdeclodimhou01ds3p.png-119.3kB

image_1cn8gr0igoqlafl84padn1vsp2m.png-157.7kB

  1. # P11 管理工作空间
  2. setwd("D:/...") # 或 setwd("D:\\...\\Ch01") 设定工作目录
  3. rm(list=ls()) # 清内存
  4. options()
  5. options(digits=3)
  6. x <- runif(20)
  7. summary(x)
  8. hist(x)
  9. # P17 包的安装与调用
  10. help.start()
  11. install.packages("vcd") # 批量安装 install.packages(c("zoo","xts"))
  12. help(package="vcd")
  13. library(vcd)
  14. help(Arthritis)
  15. Arthritis
  16. example(Arthritis)

2. 创建数据集

data.jpg-35.2kB

  1. # P21 创建向量
  2. a <- c(1, 2, 5, 3, 6, -2, 4)
  3. b <- c("one", "two", "three")
  4. c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
  5. # P21 向量下标
  6. a <- c(1, 2, 5, 3, 6, -2, 4)
  7. a[3]
  8. a[c(1, 3, 5)]
  9. a[2:6]
  10. a <- c("k", "j", "h", "a", "c", "m")
  11. a[3]
  12. a[c(1, 3, 5)]
  13. a[2:6]
  14. # P22 创建矩阵
  15. y <- matrix(1:20, nrow=5, ncol=4)
  16. y
  17. cells <- c(1,26,24,68)
  18. rnames <- c("R1", "R2")
  19. cnames <- c("C1", "C2")
  20. mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE,
  21. dimnames=list(rnames, cnames))
  22. mymatrix
  23. mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=FALSE,
  24. dimnames=list(rnames, cnames))
  25. mymatrix
  26. # P22 矩阵下标
  27. x <- matrix(1:10, nrow=2)
  28. x
  29. x[2,]
  30. x[,2]
  31. x[1,4]
  32. x[1, c(4,5)]
  33. # P23 创建数组
  34. dim1 <- c("A1", "A2")
  35. dim2 <- c("B1", "B2", "B3")
  36. dim3 <- c("C1", "C2", "C3", "C4")
  37. z <- array(1:24, c(2,3,4), dimnames=list(dim1, dim2, dim3))
  38. z
  39. # P24 创建数据框
  40. patientID <- c(1, 2, 3, 4)
  41. age <- c(25, 34, 28, 52)
  42. diabetes <- c("Type1", "Type2", "Type1", "Type1")
  43. status <- c("Poor", "Improved", "Excellent", "Poor")
  44. patientdata <- data.frame(patientID, age, diabetes, status)
  45. patientdata
  46. # P24 选择数据框中的元素
  47. patientdata[1:2]
  48. patientdata[c("diabetes","status")]
  49. patientdata$age
  50. # P28 因子的使用
  51. patientID <- c(1, 2, 3, 4)
  52. age <- c(25, 34, 28, 52)
  53. diabetes <- c("Type1", "Type2", "Type1", "Type1")
  54. status <- c("Poor", "Improved", "Excellent", "Poor")
  55. diabetes <- factor(diabetes)
  56. status <- factor(status, order=TRUE)
  57. patientdata <- data.frame(patientID, age, diabetes, status)
  58. str(patientdata)
  59. summary(patientdata)
  60. # P29 创建列表
  61. g <- "My First List"
  62. h <- c(25, 26, 18, 39)
  63. j <- matrix(1:10, nrow=5)
  64. k <- c("one", "two", "three")
  65. mylist <- list(title=g, ages=h, j, k)
  66. mylist
  67. # P31 键盘输入
  68. mydata <- data.frame(age=numeric(0),
  69. gender=character(0), weight=numeric(0))
  70. mydata <- edit(mydata)
  71. # P32 直接录入
  72. mydatatxt <- "
  73. age gender weight
  74. 25 m 166
  75. 30 f 115
  76. 18 f 120
  77. "
  78. mydata <- read.table(header=TRUE, text=mydatatxt)
  79. # P33 读入带分隔符的文本文件(.csv)
  80. grades <- read.table("studentgrades.csv", header=TRUE,row.names="StudentID", sep=",")
  81. grades # print data frame
  82. str(grades) # view data frame structure
  83. # P34 指定.csv文件的变量类别
  84. grades <- read.table("studentgrades.csv", header=TRUE,
  85. row.names="StudentID", sep=",",
  86. colClasses=c("character", "character", "character",
  87. "numeric", "numeric", "numeric"))
  88. grades # print data frame
  89. str(grades) # view data frame structure

3. 补充阅读

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注