未明学院14天打卡笔记 Day3

#矩阵
m<-matrix(1:6,nrow=2,ncol=3,byrow =T,dimnames = list(c('r1','r2'),c('c1','c2','c3')))
#行名,因为是有两行,所以要是用向量,有三列,所以也是用向量
m
class(m)
#访问数据
m[1,1]
m[1,]
m[,1]
m[c(T,F),]
m['r1',]
m[1,]<-c(100,99,98)
m
m[1,]<-100
m

#转置
t(m)

#数组
array(1:10)
array(1:10,dim = c(2,5))

dim1<-c('A1','A2')
dim2<-c('B1','B2','B3')
dim3<-c('c1','c2')
array(1:12,dim=c(2,3,2),dimnames = list(dim1,dim2,dim3))
array3<-array(1:12,dim=c(2,3,2),dimnames = list(dim1,dim2,dim3))
array3[,,'c1'] ###返回的其实就是一个矩阵
array3[,,c(T,F)]


##数据框
data.frame()
student<-data.frame(ID=c(1001,1002,1003),
name=c('Lili','Bob',"Sam"), ##不能少最后面的逗号
GENDER=c('F','M','M'),stringsAsFactors = F)##将自动转化为因子类型的转换成字符串型
student
student[1,1]
student[1,]
student[,1]
student[,'ID']##输出的是向量
student['ID']##输出的是数据框
student$ID##输出向量
student[c('ID','name')]
student[1,1]<-1009
student

##列表 可以含有不同类型的元素
list1<-list(a=1,b='char',c=m,d=student)
list1
list1[[4]] ##访问时必须加双重的括号
list1[['d']]
##若加单重括号会咋样呢
list1[4] ##会多出一个$,
class(list1[4]) ##单括号得到的是列表,而不再是原来的数据框

# 作业 ----------------------------------------------------------------------

#1、
matrixA<-matrix(1:12,nrow=3,ncol=4,byrow=F)
matrixB<-matrix(1:12,nrow=3,ncol=4,byrow=T)

##2、
matrixB[,1]<-100

扫描二维码关注公众号,回复: 7344456 查看本文章


#3、
wtdata<-data.frame(ID=c(1,2,3),name=c('张三','李四','王五'),weight=c(60,70,80),stringsAsFactors = F)

#4、
wtdata[c(T,F,T),,]

#5、
wtlist<-list(a=matrixA,b=matrixB,c=wtdata)

#6、
wtlist[1]

猜你喜欢

转载自www.cnblogs.com/chayibaishanlqy/p/11574846.html