rep(1:4, c(2,2,2,2)) # same as second.
rep(1:4, c(2,1,2,1))
rep(1:4, each = 2, len = 4) # first 4 only.
rep(1:4, each = 2, len = 10) # 8 integers plus two recycled 1's.
rep(1:4, each = 2, times = 3) # length 24, 3 complete replications
rep(1, 40*(1-.8)) # length 7 on most platforms
rep(1, 40*(1-.8)+1e-7) # better
## replicate a list
fred <- list(happy = 1:10, name = \rep(fred, 5)
# date-time objects x <- .leap.seconds[1:3] rep(x, 2)
rep(as.POSIXlt(x), rep(2, 3))
## named factor
x <- factor(LETTERS[1:4]); names(x) <- letters[1:4]
rep(x, 2)
rep(x, each = 2)
rep.int(x, 2) # no names
rep> rep_len(x, 10)
#2.11 产生字母序列#### letters[1:30]
#2.12 which()函数#### a<-rnorm(10) a
which.max(a) #返回数组中最大的数的位置 which.min(a) #返回数组中最小的数的位置 a[which.max(a)]
which(a==a[which.max(a)]) which(a>0) a[which(a>0)]
#2.13排序函数#### a<-1:10 a
rev(a) #反转顺序
a<-c(3,1,5,6,9,2,7,4,6,5) sort(a) rev(sort(a))
#2.14生成矩阵#### a<-c(1:36) a
a1<-matrix(a,nrow = 9, ncol = 4);a1 a2<-matrix(a,nrow = 4, ncol = 9);a2
a3<-matrix(a,nrow = 9, ncol = 4, byrow = TRUE);a3 dim(a1) #查看函数的维度 dim(a2)[1] #行 dim(a3)[2] #列
#矩阵运算 t(a1) #转置
a1+a2 a1+a3
#2.15改变RStudio的工作界面(通过tools里面的layout设置完成)####
#2.16 设定工作目录,方便载入数据,将数据存放在设定的目录中,载入数据时就不用指定路径了####
setwd(\setwd(\
getwd() #用于查看当前工作目录
#2.17 help()函数#### help(\?seq
help(package=\
#2.18 example()函数#### example(\example(\example(\
#搜索自己需要的函数
help.search(\
#2.19 数据框 dataframe####
##数据框就是一个数据表格,一行表示一个记录,一列表示一个变量 x1<-c(1,2,3,4,5,6,7,8,9) x2<-c(2,4,6,8,10,12,14,16,18) x<-data.frame(x1,x2);x
x=data.frame('weight'=x1, 'cost'=x2);x
x$\ #数据框可以利用已有的变量产生新的变量并存储于当前数据框中
names(x) #查看数据框中的变量
str(x) #查看数据框中数据的定义
#我们在载入数据后,要使用上面的两个函数来检查数据载入是否成功,并检查数据的定义
#2.20 列表list() ####
x1<-1 #单个数字,其实是一个只有一个元素的一维向量
x2<-c(1,2,3,4)
x3<-c(\ #字符向量
x4<-matrix(1:36,nrow = 9,ncol = 4) #矩阵,vector
x5<-data.frame(a=c(1,2,3,4), b=c(2,3,4,5),c=c(3,4,5,6))
list_01<-list(x1=x1,x2=x2,x3=x3,x4=x4,x5=x5) #产生一个数据框
list_01$x3
###3. 数据导入与导出 ####
#3.1 先把R内置的CO2数据集导出,然后再联系导入操作#### write.table(CO2,file=\二氧化碳.txt\write.csv(CO2,file = \二氧化碳.csv\
#3.2 导入逗号分割的文本文件####
read.table(file = \二氧化碳.txt\#最好指定一个名称
carbon<-read.table(file = \二氧化碳.txt\
#3.3 导入csv格式的数据####
carbon_csv<-read.csv(file = \二氧化碳.csv\
#3.4通过剪贴板读入数据并写入文件 setwd(\
相关推荐: