R入门练习

参考这篇博客练习R的基本操作
一、导入数据
1 #1数据输入
2 install.packages(“xlsx”)
3 library(xlsx)
4 workbook<-“F:/Machine Learning/R Basic/Rtest1.xlsx”
5 StuScore<-read.xlsx(workbook,1)#1表示sheet1
6 StuScore

二、数据预处理,将数据标准化

2 > options(digits=2)#限定为2位小数
 3 > afterscale<-scale(StuScore[,3:5])
 4 > afterscale

5 Math Science English
6 [1,] -0.58 1.040 0.20
7 [2,] -1.02 -0.815 -1.17
8 [3,] 0.82 -0.086 -0.12
9 [4,] 0.28 -0.881 -0.54
10 [5,] -1.15 1.106 -0.86
11 [6,] 0.98 0.643 0.73
12 [7,] 0.29 -0.086 1.47
13 [8,] -1.54 -1.544 -1.17
14 [9,] 0.56 -0.749 -0.12
15 [10,] 1.35 1.372 1.57
16 attr(,”scaled:center”)
17 Math Science English
18 551 79 23
19 attr(,”scaled:scale”)
20 Math Science English
21 84.7 15.1 9.5
三、函数quantile()给出学生综合得分的百分位数

afterquantile<-quantile(score,c(.8,.6,.4,.2))

afterquantile
80% 60% 40% 20%
0.43 0.15 -0.13 -0.51

四、将分数转成等级

 StuScore$grade[score>=afterquantile[1]]<-"A"
 > StuScore$grade[score<afterquantile[1]&&score>=afterquantile[2]]<-"B"
  > StuScore$grade[score<afterquantile[2]&&score>=afterquantile[3]]<-"C"
 > StuScore$grade[score<afterquantile[3]&&score>=afterquantile[4]]<-"D"
  > StuScore$grade[score<afterquantile[4]]<-"E"
 > StuScore

数据弄得有点奇怪。。

五、使用strsplit()以空格为界把学生姓名拆分为姓氏和名字

StuScore StuName<as.character(StuScore StuName)
name<-strsplit(StuScore$StuName,” “)

六、把name分成Firstname和LastName,加入到StuScore中
#7把name分成Firstname和LastName,加入到StuScore中

> FirstName<-sapply(name,"[",1)
  > LastName<-sapply(name,"[",2)
  > StuScore<-cbind(FirstName,LastName,StuScore[,-1])

七、order排序
StuScore[order(LastName,FirstName),]
八、绘制成条形图
代码与博客中的一样,为了显示效果,对参数稍作调整
这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值