美文网首页
1.10 R_basic_code 'old is new'

1.10 R_basic_code 'old is new'

作者: dandanwu90 | 来源:发表于2019-01-10 17:38 被阅读0次

day_4_code

seq(1,20)
c(1:20)==seq(1,20)

cbind and rbind, compare the difference between m1,m2

a1<- c(1:5)
a2<- c(6:10)
m1<- cbind(a1,a2)
m2<- rbind(a1,a2)
m3<- merge(a1,a2,by='*')

t.data.frame transpose row and column,compare test and test1 below

cancerID <- c("HCC","NSCLC","BC")
targetdrug <- c("sorafenib","gefitinib","Herceptin")
test<-data.frame(cancerID,targetdrug)
test1<-t.data.frame(test)

set.seed save and store random number generation in R,combined with sample

set.seed(811)
f <- sample(seq(1,100), 10)
f

sort and order: sort vector or factor into ascending or descending order; order permutation of first argument

sort(f)
order(f)
f[order(f)] 

index/indices (which 函数中,arr.ind = T表示是array时,是否返回坐标,useNames=T 按行数位置)

which(f==28)  ##数字28的位置
f[which(f==28)]## 告诉我(数字28的位置=2)位置上的数字 

查找后返回位置,grep返回坐标,grepl返回逻辑值

d <- c("TP53","ERBB2","BRCA1")
grep("ERBB2",d)  ## grep向量d中的字符"ERBB2"  返回为坐标           
grepl("ERBB2",d) ##逻辑判断

dim(exprSet) ## 查看数据框维度1208行,1000列

subset函数

test <- subset(test,test$subgroup == "Normal",select =c(1:8))
test1 <- test[,-1]   # 删掉第一列
##调整顺序,将原来的1-8,调整为8,1-7
test1 <- test1[,c(8,1:7)]

which give the TRUE indices of a logical object, allowing for array indices
arrayInd opposite function from which, should array indices be returned.

For getting the data in ma[2,3], which code is right

arrayInd(ind = ma, .dim = ma[2,3],  .dimnames = NULL,useNames = F)
arrayInd(ind = ma[2,3], .dim = ma[2,3],  .dimnames = NULL,useNames = F)
arrayInd(ind = ma[2,3], .dim = ma,  .dimnames = NULL,useNames = F)
arrayInd(ind = ma, .dim = ma,  .dimnames = NULL,useNames = F)

strsplit(rownames(exprSet)[1],split = "") 分割多个字符时返回类型为list,若只有一个值时,返回为向量,可以用class()查看
unlist(strsplit(rownames(exprSet)[1],split = ""))返回类型为character,查看方法同上,

exprSet数据框列名的第一个元素字符数进行统计
nchar(rownames(exprSet)[1])
substring去字符串
substring(rownames(exprSet)[1],14,15)

lapply返回列表
sapply返回向量

split_math <- strsplit(pioneers, split = ":")
split_math[[1]]
class(split_math[[1]])
pioneers[1]  #取该向量的第一个
pioneers[[1]] #向量数据已经处在最低级,无法再降级

相关文章

网友评论

      本文标题:1.10 R_basic_code 'old is new'

      本文链接:https://www.haomeiwen.com/subject/nxnarqtx.html