美文网首页
数据处理-3(基于R语言)

数据处理-3(基于R语言)

作者: 北欧森林 | 来源:发表于2021-02-27 22:19 被阅读0次
  1. How to Repeat Vectors in R
rep(c(0, 0, 7), times = 3)
# [1] 0 0 7 0 0 7 0 0 7

rep(c(2, 4, 2), each = 3)
# [1] 2 2 2 4 4 4 2 2 2

rep(c(0, 7), times = c(4,2))
# [1] 0 0 0 0 7 7

rep(1:3,length.out=7)
# [1] 1 2 3 1 2 3 1
  1. 很简单的一步,chr 变 numeric,需要as.numeric(as.character(x)) ,这样数据才不会出错。
  2. if(!dir.exists("clinical"))dir.create("clinical") 用于创建文件夹(先判断其是否存在)
  3. sum(x1 > 1) > 9 -- sum 先统计括号里逻辑值为TRUE的个数,然后再返回一个逻辑值
    apply(exp, 1, function(x) sum(x > 1) > 9)
  4. browseVignettes() 查看某个R包的长篇使用文档,如:browseVignettes("clusterProfiler");
    如果希望留在命令行中,则可以使用 vignette() 函数。
vignette(package = "ggplot2")
# Vignettes in package ‘ggplot2’:

# ggplot2-specs           Aesthetic specifications (source, html)
# extending-ggplot2       Extending ggplot2 (source, html)

vignette("ggplot2-specs") # 在help文档里查看具体说明文字
  1. fix(mydata)函数可以调出数据编辑器,像在excel那样查看、修改数据和函数
  2. R语言里的小数点:
# Method1: 
round(mydat,3) #小数点后3位
##  [1] 0.008

# Method2:format()函数实现科学计数法与普通数字的自由转换,但应注意其输出结果是字符串
format(mydat, scientific = F)
##  [1] "0.0082064320"
format(x, scientific = T,digits = 3)
##  [1] "8.21e-03"
## source: 微信公众号:生信星球
  1. 𝑄3和𝑄1的差距称为四分位距(InterQuartile Range, IQR):Q3-Q1
    上界/下界,英文名为Upper Whisker和Lower Whisker,有时又被称为最大值和最小值,但是这样容易引起误解,因为它们并非数据集里面的最大值或最小值,而是去除所有离群值和极端值之后的最小值和最大值。
    离群值(mild outlier)和极端值(extreme outlier)有时统称为离群值或异常值(outlier)。处于内限以外位置的点表示的数据都是异常值,其中在内限与外限之间的异常值为温和的异常值 mild outliers,在外限以外的为极端的异常值 extreme outliers。(source: https://www.biaodianfu.com/boxplot.html)
image.png
  1. Boxplot绘图中的离群值Outliers的去除:
#得到离群值的索引:
OutVals = boxplot(x)$out #离群值
which(x %in% OutVals) #索引

# 删除离群值:
x[! x %in% OutVals]

# 不想绘图,只想得到结果可以这样:
OutVals = boxplot(x, plot=FALSE)$out

10.要从test数据集中筛选出所有variable name里包含“aa”的observation:

test[grep("aa", test$name), ]

相关文章

网友评论

      本文标题:数据处理-3(基于R语言)

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