学习小组Day5笔记--尤书书

Day5-数据结构.png

(1)显示工作路径 getwd()
(2)要理解其中的命令、函数的意思。函数或者命令不会用时,除了百度/谷歌搜索以外,用这个命令查看帮助:?read.table,调出对应的帮助文档,翻到example部分研究一下。
(3)数据类型
向量(vector)👈重要(向量是由元素组成的,元素可以是数字或者字符串。);
数据框(Data frame)👈重要(表格在R语言中改名叫数据框);
矩阵(Matrix)
数组(Array)

向量

标量:一个元素组成的变量
向量:多个元素组成的变量(有序排列)

赋值

> x <- c(1,2,3) #常用的向量写法,意为将x定义为由元素1,2,3组成的向量。
> x
[1] 1 2 3
> x<-1:10 #从1-10之间所有的整数
> x
 [1]  1  2  3  4  5  6  7  8  9 10
> x<-seq(1,10,by=0.5) #1-10之间每隔0.5取一个数
> x
 [1]  1.0  1.5  2.0  2.5  3.0  3.5  4.0  4.5
 [9]  5.0  5.5  6.0  6.5  7.0  7.5  8.0  8.5
[17]  9.0  9.5 10.0
> x<-rep(1:3,times=2) #1-3 重复2次
> x
[1] 1 2 3 1 2 3

从向量中提取元素

x int[1:6] 1 2 3 1 2 3

(1)根据元素位置

> x[4]
[1] 1
> x[-4]
[1] 1 2 3 2 3
> x[2:4]
[1] 2 3 1
> x[-(2:4)]
[1] 1 2 3
> x[c(1,5)]
[1] 1 2

(2)根据值

> x[x==10]
integer(0)
> x[x==1]
[1] 1 1
> x[x<0]
integer(0)
> x[x<3]
[1] 1 2 1 2
> x[x%in%c(1,2,5)]
[1] 1 2 1 2

数据框

读取本地数据

> read.table(file="huahua.txt", sep="\t",header=T)
  X1 X2
1  A  1
2  B NA
3  C NA
4  D  3
5  E NA
> a<-read.table(file="huahua.txt", sep="\t",header=T)

read.table(file, header = FALSE, sep = "", quote = ""'",
dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"),
row.names, col.names, as.is = !stringsAsFactors,
na.strings = "NA", colClasses = NA, nrows = -1,
skip = 0, check.names = TRUE, fill = !blank.lines.skip,
strip.white = FALSE, blank.lines.skip = TRUE,
comment.char = "#",
allowEscapes = FALSE, flush = FALSE,
stringsAsFactors = default.stringsAsFactors()
fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)

sep
the field separator character. Values on each line of the file are separated by this character. If sep = "" (the default for read.table) the separator is ‘white space’, that is one or more spaces, tabs, newlines or carriage returns.

header
a logical value indicating whether the file contains the names of the variables as its first line. If missing, the value is determined from the file format: header is set to TRUE if and only if the first row contains one fewer field than the number of columns.

设置行名和列名

> X<-read.csv('doudou.txt')
> X
  X1 X2
1  A  1
2  B NA
3  C NA
4  D  3
5  E NA
> colnames(X)
[1] "X1" "X2"
> rownames(X)
[1] "1" "2" "3" "4" "5"
> colnames(X)[1]<-"bioplanet"
> colnames(X)
[1] "bioplanet" "X2"       
> X<-read.csv(file = "huahua.txt",sep = "   ",header =T,row.names=1)
> X
  X2
A  1
B NA
C NA
D  3
E NA

数据框的导出

write.table(X,file = "yu.txt",sep = ",",quote=F)#分隔符改为逗号,字符串不加双引号(默认格式带由双引号) 
image.png

变量的保存与重新加载

save.image(file="bioinfoplanet.RData")#保存当前所有变量
save(X,file="test.RData")#保存其中一个变量
load("test.RData")#再次使用RData时的加载命令
image.png

提取元素

> X<-read.csv('doudou.txt')
> X
  X1 X2
1  A  1
2  B NA
3  C NA
4  D  3
5  E NA

> X[1,2]
[1] 1

> X[3,]
  X1 X2
3  C NA

> X[,2]
[1]  1 NA NA  3 NA

> X[2]
  X2
1  1
2 NA
3 NA
4  3
5 NA

> X[1:2]
  X1 X2
1  A  1
2  B NA
3  C NA
4  D  3
5  E NA

> X[c(1,2)]
  X1 X2
1  A  1
2  B NA
3  C NA
4  D  3
5  E NA

> X$X2
[1]  1 NA NA  3 NA

【选修部分】直接使用数据框中的变量

> options(stringsAsFactors = T)
> a <-data.frame(case=paste0("S",1:9),values=runif(9))
> a
  case     values
1   S1 0.90779736
2   S2 0.71654872
3   S3 0.68911164
4   S4 0.28194740
5   S5 0.01935258
6   S6 0.35217174
7   S7 0.09469227
8   S8 0.58675009
9   S9 0.95823204
> plot(a$case,a$values)
image.png

环境设置函数为options(),用options()命令可以设置一些环境变量,使用help(options)可以查看详细的参数信息。
R语言数据框中的stringsAsFactors参数

为了避免重复地键入对象名称,不能允许数据框名出现两次。

方法1:attach

> plot(a$case,a$values)
> attach(a)
> plot(case,values)
> detach(a)
> plot(case,values)
Error in plot(case, values) : 找不到对象'case'

方法2:with

> with(a,{
+     plot(case,values)
+     x<<-summary(values)  #求和并赋值给x,<<的意思是作为全局变量,也就是出了大括号仍有效。
+ })

> x
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.01935 0.28195 0.58675 0.51184 0.71655 0.95823 
>with的用法1
> with(mtcars,{
+ summary(mpg,disp,wt)
+ plot(mpg,disp)
+ plot(mpg,wt)
+ })
#大括号{}之间的语句都只针对数据框mtcars执行,但如果大括号中只有summary(mpg)一句的话,则省略大括号。
with的用法2
> with(mtcars,{
+ stats<-summary(mpg)
+ stats})
   Min. 1st Qu. Median Mean 3rd Qu. Max.
  10.40 15.42 19.20 20.09 22.80 33.90

学会保存脚本

image.png

另外请在作业中回答一个问题:save(X,file="test.RData")这句代码如果报错object X not found,是为什么,应该怎么解决?

X没有赋值。用X<-语句给X赋值。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容