Day 5 - 数据结构
Vector
图片来源:生物星球
eg.
x <- c(1,2,3)
x <- seq(1,10, by = 0.5) # 1-10 之间每隔0.5取一个数
x <- rep(1:3, times = 2) # 1-3 重复2次
- 从向量中提取元素
- 根据元素位置
- x[4] # x第4个元素
- x[-4] # 除了第4个元素之外剩余的元素
- x[2:4] # 第2到4个元素
- x[-(2:4)] # 除了第2-4个元素
- x[c(1,5)] # 第1个和第5个元素
- 根据值
- x[x==10] #等于10的元素
- x[x<0]
- x[x %in% c(1,2,5)] #存在于向量c(1,2,5)中的元素
Data frame
read.table(file = "huahua.txt", sep = "\t", header =T)
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.
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.
设置行列名
colnumes, rownames数据框的导出
write.table(x,file ="yu.txt", sep =",", qupte =F)变量的保存与重新加载
格式:RData
save.image(file = "bioinfoplanet.RData")
保存当前所有变量
save(x, file = "test.RData")
保存其中一个变量
load(test.RData")
再次使用RData时的加载命令
- 提取元素
x[x,y]
第x行第y列
x[x,]
第x行
x[,y], or x[y]
第y列
x[a:b]
第a列到第b列
x[c(a,b)]
第a列和第b列
x$列名
提取某列
如果oject X not found, 应该是变量X 没有赋值