R语言数据结构实例入门

R语言的数据结构介绍"##"代表输出内容
		向量
			储存一维数据、其中数据数据类型相同、用C()创建向量。
			例:
			v.number<- c(1,2,3,4,5,6)
			v.char<- c(”tom”,”jack”,”mack”,”anna”)
			v.bool<- c(TRUE,FALSE,FALSE)
			例1(真假问题):
			if(-1) print("true") #-1为真
			##[1] true
			if(0) print("false") #无输出
			if() print("fasle") #报错,无法比较
			##Error:missing value where TRUE/FALSE needed
			if(NULL) print("false")#报错,无法比较
			##Error:atgument is of length zero
			例2(数据类型转换问题):
			x<- c(1:10,"jane")
			x
			##[1] "1"    "2"    "3"    "4"    "5"    "6"    "7"    "8"    "9"    "10"  
			[11] "jane"
			例3(数据类型转换问题):
			x<-c(1:10,"jace")
			class(x)
			##[1] "character"

			y<-c(1:10)
			class(y)
			##[1] "integer"
			数据转换类型次序:NULL->raw->logical->integer->complex->character->list->expression
			例4(下标问题):
			x<-c(1:10)
			x[0]
			##integer(0)
			x[1]
			##[1] 1
			x[2:4]
			##[1] 2 3 4
			x[-1]
			##[1]  2  3  4  5  6  7  8  9 10
			x[11]
			##[1] NA
			变量命名问题:如x,x.1,"1",.x,._等合法,.1不合法
		矩阵
			语法:matrix(data=NA,nrow=1,ncol=1,byrow=FALSE,dimnames=NULL)
			例(创建矩阵):
			rowName<-paste("row",1:3,sep='') 
			colName<-paste("col",1:4,sep='') 
			(ml<-matrix(data=1:12,nrow=3,byrow=TRUE,dimnames=list(rowName,colName)))
			##     col1 col2 col3 col4
			##row1    1    2    3    4
			##row2    5    6    7    8
			##row3    9   10   11   12
			(m2<-matrix(data=1:12,nrow=3,byrow=FALSE,dimnames=list(rowName,colName)))
			##     col1 col2 col3 col4
			##row1    1    4    7   10
			##row2    2    5    8   11
			##row3    3    6    9   12
			ml[1,2]
			##[1] 2
			ml[1,2:4]
			##col2 col3 col4 
			##   2    3    4 
			ml[,1]
			##row1 row2 row3 
			##   1    5    9
			ml[3]
			##[1] 9



		数组
			例(创建数组):
			> dim1name<-paste("A",1:3,sep='')
			> dim2name<-paste("B",1:4,sep='')
			> dim3name<-paste("C",1:2,sep='')
			> (a<-array(1:24,dim=c(3,4,2),dimnames=list(dim1name,dim2name,dim3name)))
			, , C1

			   B1 B2 B3 B4
			A1  1  4  7 10
			A2  2  5  8 11
			A3  3  6  9 12

			, , C2

			   B1 B2 B3 B4
			A1 13 16 19 22
			A2 14 17 20 23
			A3 15 18 21 24

			> a[1,1,1]
			[1] 1
			> a[1,2,]
			C1 C2 
			 4 16 
			> a[1,,]
			   C1 C2
			B1  1 13
			B2  4 16
			B3  7 19
			B4 10 22
			> a[,1,]
			   C1 C2
			A1  1 13
			A2  2 14
			A3  3 15


		因子
			语法:factor(x=character(),levels,labels=levels,exclude=NA,ordered=is.ordered(x),nmax=NA)
			      levels(x)
			例:
			> chr<-c("f","e","d","c","b","a")
			> f<-factor(chr)
			> class(f)
			[1] "factor"
			> storage.mode(f)
			[1] "integer"
			> as.numeric(f)
			[1] 6 5 4 3 2 1
			> levels(f)
			[1] "a" "b" "c" "d" "e" "f"
		列表
			语法:list(x1,x2,x3,....)
			例(创建列表):
			i<-1
			v<-c(1:10)
			ch<-c("ja","ju")
			a<-array(1:12,c(2,3,4))
			li<-list(i=i,v=v,ch=ch,a=a)
			
			> li[1]
			$i
			[1] 1

			> li[[1]] [1]
			[1] 1
			> li["a"]
			$a
			, , 1

			     [,1] [,2] [,3]
			[1,]    1    3    5
			[2,]    2    4    6

			, , 2

			     [,1] [,2] [,3]
			[1,]    7    9   11
			[2,]    8   10   12

			, , 3

			     [,1] [,2] [,3]
			[1,]    1    3    5
			[2,]    2    4    6

			, , 4

			     [,1] [,2] [,3]
			[1,]    7    9   11
			[2,]    8   10   12


			> li$a
			, , 1

			     [,1] [,2] [,3]
			[1,]    1    3    5
			[2,]    2    4    6

			, , 2

			     [,1] [,2] [,3]
			[1,]    7    9   11
			[2,]    8   10   12

			, , 3

			     [,1] [,2] [,3]
			[1,]    1    3    5
			[2,]    2    4    6

			, , 4

			     [,1] [,2] [,3]
			[1,]    7    9   11
			[2,]    8   10   12

			> li$c
			[1] "ja" "ju"
			> li["c"]
			$<NA>
			NULL
			例(list嵌套):
			> li<-list(list(list(x=1,y=2)))
			> li
			[[1]]
			[[1]][[1]]
			[[1]][[1]]$x
			[1] 1

			[[1]][[1]]$y
			[1] 2



			> class(li[[1]])
			[1] "list"
			> class(li[[1]][[1]])
			[1] "list"
			> class(li[[1]][[1]][1])
			[1] "list"
			> class(li[[1]][[1]][[1]])
			[1] "numeric"

		数据框
			语法:data.frame(name=name,...,height=height,row.names = NULL,check.rows=FALSE,check.names=TRUE,stringsAsFactors=default.stringAsFactors())
			创建数据框:
			> name<- c("cc","bb","b","a")
			> age<-c(11,12,13,14)
			> gender<-c("F","M","M","M")
			> height<-c(90,100,98,89)
			> student<-data.frame(name=name,age=age,gender=gender,height=height,row.names = name)
			> student
			   name age gender height
			cc   cc  11      F     90
			bb   bb  12      M    100
			b     b  13      M     98
			a     a  14      M     89
			> student[1,]
			   name age gender height
			cc   cc  11      F     90
			> student[1,3]
			[1] F
			Levels: F M
			> student$name
			[1] cc bb b  a 
			Levels: a b bb cc




	数据的导入与导出
		键盘 导入,从纯文本中读取数据,其他文件中读取数据,写文件,使用windows的粘贴板功能,保存输出,保存为R的特有的格式。
	总结与补充
		总结,补充。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值