R Manage Data

来源:互联网 发布:大数据分析导论 感想 编辑:程序博客网 时间:2024/06/09 15:28

R read and save()

读取文件

> x = read.table("1.txt")> x   V1   V21 134    22  23    33  12    34 234    25  23 null6 123 null7 123    18  23   23
> a = c(1:10)> save(a,file = "a.RData")> save.image()

第二次打开 若想清空变量历史记录

rm(list = ls())

循环

for

> a=0> for(i in 1:20) {a[i] = i*2+3}> a [1]  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43> b=0> for(i in 1:20) {a[i] = i*2+3; b[i] = i*10-1}> b [1]   9  19  29  39  49  59  69  79  89  99 109 119 129 139 149 159 169 179 189 199

while

> a = 0> a[1] = 3> i = 1> while (a[i] < 33) {i=i+1; a[i]=a[i-1]+6}> a[1]  3  9 15 21 27 33

rep() 后面是重复的次数

> rep(1:3,5) [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3> a [1]  3  9 15 21 27 33> rep(a,5) [1]  3  9 15 21 27 33  3  9 15 21 27 33  3  9 15 21 27 33  3  9 15 21 27 33  3  9 15 21 27 33

source() 运行脚本 脚本中需要有print()

Example

Data : 学号+三科成绩

  • rnorm() 正太分布
  • runif() 均匀分布
  • round() 四舍五入
> x1 = round(runif(100,min = 80, max = 100))> x1  [1]  89  87  97  82  99  87  98  82  90  83  95  90  90  92  98  80  93  97  81  86  93  85  84 [24]  85  95  84  93  88  81  97  99  81  85 100  81  87  84  95  81  98  88  82  85  98  85  83 [47]  95  82  98  93  82  88  94 100  81  91  84  84  86  97  93  85  95  87  85  97  96  87  86 [70]  94  87  92  96  95  94  96  94  81  91  82 100  87  92  97  90  87  84  83  86  84  89  82 [93]  91  84  86  85  97  94  89  94> x2 = round(rnorm(100, mean = 80, sd = 7))> x2  [1] 82 77 78 87 80 77 80 81 66 85 78 67 86 75 94 78 74 87 76 83 77 74 79 90 90 84 79 74 83 67 93 [32] 87 82 89 77 92 90 80 95 78 91 87 75 81 74 77 82 82 83 91 80 74 86 86 89 84 86 81 74 82 80 89 [63] 82 85 84 80 77 97 82 79 72 78 83 78 90 76 84 83 80 87 85 80 70 85 70 87 82 78 72 71 79 87 82 [94] 84 81 75 92 77 86 81> x3 = round(rnorm(100,mean = 83, sd = 18))> x3  [1]  76 105  82 117  77  57  95  69 103  72  79 106  95  56  87 105 100  69  57  90 100  93 111 [24]  75  97  92  38  54  95  80  55  95  60  72  67  73  82 109  68 109  86  88  94  79  99 109 [47] 106  81  94  73  73  76  78  81 100  92  64 101  48  82  93  63  84 111  84  96  66  72  74 [70]  65 101  91  90  91  83  90  78  61  81  97  72  83  87 101  80  76  90 110  64  80  70 106 [93]  50  96  78  72  99  80  95  71> x3[which(x3>100)] = 100> x3  [1]  76 100  82 100  77  57  95  69 100  72  79 100  95  56  87 100 100  69  57  90 100  93 100 [24]  75  97  92  38  54  95  80  55  95  60  72  67  73  82 100  68 100  86  88  94  79  99 100 [47] 100  81  94  73  73  76  78  81 100  92  64 100  48  82  93  63  84 100  84  96  66  72  74 [70]  65 100  91  90  91  83  90  78  61  81  97  72  83  87 100  80  76  90 100  64  80  70 100 [93]  50  96  78  72  99  80  95  71

加上学生的学号 做成数据框

> num = seq(10378001,10378100)> x = data.frame(num,x1,x2,x3)> x         num  x1 x2  x31   10378001  89 82  762   10378002  87 77 1003   10378003  97 78  824   10378004  82 87 1005   10378005  99 80  776   10378006  87 77  577   10378007  98 80  95

统计

  • colMeans() 对列平均
  • apply() 对x, 2代表列方向,mean平均值
> colMeans(x)        num          x1          x2          x3 10378050.50       89.42       81.46       82.47 > colMeans(x)[c("x2","x3")]   x2    x3 81.46 82.47 > apply(x,2,max)     num       x1       x2       x3 10378100      100       97      100 

求每一个人的总分

> apply(x[c("x1","x2","x3")],1,sum)  [1] 247 264 257 269 256 221 273 232 256 240 252 257 271 223 279 258 267 253 214 259 270 252 263 [24] 250 282 260 210 216 259 244 247 263 227 261 225 252 256 275 244 276 265 257 254 258 258 260 [47] 277 245 275 257 235 238 258 267 270 267 234 265 208 261 266 237 261 272 253 273 239 256 242 [70] 238 259 261 269 264 267 262 256 225 252 266 257 250 249 282 240 250 256 261 222 235 238 269 [93] 223 264 245 232 288 251 270 246

取出总分最高的学生的学号num

> which.max(apply(x[c("x1","x2","x3")],1,sum))[1] 97> x$num(which.max(apply(x[c("x1","x2","x3")],1,sum)))//错误: 不适用于非函数> x$num[which.max(apply(x[c("x1","x2","x3")],1,sum))][1] 10378097

直方图Histogram 分布

hist(x1)

散点图 两科成绩的关系

plot(x1,x2)plot(x$x1,x$x2)plot(x$x1,x$x2,main = "Relationship between maths and English",xlab = "English",ylab = "maths")plot(x$x1,x$x2,main = "Relationship between maths and English",xlab = "English",ylab = "maths",col="red",pch=19,xlim = c(0,100),ylim = c(0,100))

plot

连线图

> a = c(2,3,4,5,6)> b = c(4,5,6,8,12)> plot(a,b,type = "l")

列联函数table() 柱状图barplot()

> table(x$x1) 80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100   1   7   7   3   8   8   5   8   3   3   4   3   3   5   6   6   3   7   5   2   3 > barplot(table(x$x1))

饼图 pie()

pie(table(x$x1))

箱尾图

  • 箱子上下横线为样本25%和75%分位数(Q1,Q3)
  • 中间的横线为样本的中位数(Q2)
  • Q1和Q3之间50%的数据对数据分散程度比较重要,Q1和Q3的差成为四分位距(Inter Quartile Range, IQR)
  • 最高最低的线就是max和min 不能超过低于或者高于IQR的1.5倍距离 超过的(小圆圈)都是异常值
boxplot(x$x1, x$x2, x$x3)boxplot(x[2:4],col = c("red","green","blue"), notch = T)boxplot(x[2:4],col = c("red","green","blue"), horizontal = T)

notch 缺口 主要用来看清楚中位线
boxplot

星相图

  • 每个图的每个角代表变量 长度代表大小
> stars(x[2:4])> stars(x[c("x1","x2","x3")])

图像越大 各科成绩越好
stars

雷达图
draw.segments 选择是否雷达图 full选择是否整个扇形

> stars(x[2:4], full = T, draw.segments = T)

雷达图

检验变量之间的关系

  • 散点图
  • 双向交叉表

双向交叉表(two-way cross-tabulation ) 交叉表或者列联表

table()函数用在单向表中
卡方检验