记录《Bioinformatics Data Skills》中关

2019-01-18  本文已影响0人  EngineerChicken

##################2019年1月18日14:34:07##########################

example("pheatmap") #获取函数的示例
help.search("heatmap") #根据关键词搜索相关的函数
library(help="pheatmap") #查看包的详细信息
ls() #We can see objects we’ve created in the global environment 
length() #return the length of vector

Alt - on Windows 快捷生成 “<-”

特点

################2019年1月22日09:48:01#######################

> x <- c(1,2,3)
> x + 1
[1] 2 3 4
> y <- c(1,2)
> x + y #当两个元素的向量不是乘积倍的时候
[1] 2 4 4
Warning message:
In x + y : longer object length is not a multiple of shorter object length
> z[c(2, 1, 10)]
[1] 2.2 3.4 NA

It’s also possible to exclude certain elements from lists using negative indexes
(使用负号来跳过数据)

> order(z)
[1] 4 3 5 2 1
> z[order(z)]
> order(z, decreasing=TRUE)
[1] 1 2 5 3 4
> z[order(z, decreasing=TRUE)] #order返回排序后的索引
[1] 3.4 2.2 1.2 0.4 -0.4
> sort(b,decreasing = T) #返回排序后的值
  b  a1  a3  a2   c 
5.4 3.4 2.0 1.0 0.4

Again, often we use functions to generate indexing vectors for us. For example, one
way to resample a vector (with replacement) is to randomly sample its indexes using
the sample() function:
[1] https://www.jianshu.com/p/38d0a44630f8
[2] https://bbs.pinggu.org/thread-3068145-1-1.html

> set.seed(0) # we set the random number seed so this example is reproducible
> i <- sample(length(z), replace=TRUE) #replace是否放回取样
> i
[1] 5 2 2 3 5
> z[i]
[1] 1.2 2.2 2.2 0.4 1.2

NA is R’s built-in value to represent missing data.
NULL represents not having a value
-Inf, Inf These are just as they sound, negative infinite and positive infinite values.
NaN stands for “not a number,” which can occur in some computations that don’t
return numbers, i.e., 0/0 or Inf + -Inf.

> is.nan(0/0)
[1] TRUE
> x <- c()
> is.null(x)
[1] TRUE
> y <- c(1,2,3)
> is.na(y[4])
[1] TRUE

Because all elements in a vector must have homogeneous data type, R will silently coerce elements so that they have the same type.
(当构建向量时,R会自动进行数据类的强转。)

> y <- cbind(x1 = 3, x2 = c(4:1))
> y
     x1 x2
[1,]  3  4
[2,]  3  3
[3,]  3  2
[4,]  3  1
> y['x1']
[1] NA
> y[1,'x1']
x1 
 3 
> y[,'x1'] 
[1] 3 3 3 3
> summary(d$total.SNPs)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 3.000 7.000 8.906 12.000 93.000
> d$percent.GC[d$Pi > 16]
[1] 39.1391 38.0380 36.8368 36.7367 43.0430 41.1411 [...]

Thus, d[$Pi > 3, ] is identical to d[which(d$Pi > 3), ];

> d$Pi > 3
[1] FALSE TRUE FALSE TRUE TRUE TRUE [...]
> which(d$Pi > 3)
[1] 2 4 5 6 7 10 [...]

subset() takes two arguments: the dataframe to operate on, and then conditions to include a
row. With subset(), d[dPi > 16 & dpercent.GC > 80, ] can be expressed as:

$ subset(d, Pi > 16 & percent.GC > 80)
start end total.SNPs total.Bases depth [...]
58550 63097001 63098000 5 947 2.39 [...]
> subset(d, Pi > 16 & percent.GC > 80,
c(start, end, Pi, percent.GC, depth))
start end Pi percent.GC depth
58550 63097001 63098000 41.172 82.0821 2.39
58641 63188001 63189000 16.436 82.3824 3.21
58642 63189001 63190000 41.099 80.5806 1.89

#####################ggplot2##################

上一篇 下一篇

猜你喜欢

热点阅读