2020-11-22R脚本模板

2020-11-22  本文已影响0人  阿乜太帅
#!/usr/bin/Rscript
# Usage: Rscript long2wide.R . input prefixofoutput

args<-commandArgs(TRUE)
#################################################
# R包加载,这里展示跨用户/跨版本调用R包
myPaths <- .libPaths()
new <- c("/home/xiaoshuai/R/x86_64-pc-linux-gnu-library/3.5","/home/xiaofang/R/x86_64-pc-linux-gnu-library/3.4")
myPaths <- c(myPaths, new)
.libPaths(myPaths)
library("tidyr", quietly = T,lib.loc="/home/xiaoshuai/R/x86_64-pc-linux-gnu-library/3.5")

#################################################
#工作目录,传入第一个参数,即读入与输出数据所在的位置
setwd(args[1])
#setwd(".")

#################################################
#长数据的读入,传入第二个参数,即特定格式的输入数据

data <- read.table(args[2],header=F)
#data <- read.table("aaa",header=FALSE)

data <- as.data.frame(data)
colnames(data) <- c("Count","Class","Group")

#################################
#长变宽
widedata1 <- spread(data,key = "Class",value = "Count")
widedata1[is.na(widedata1)]<-0
widedata2 <- spread(data,key = "Group",value = "Count")
widedata2[is.na(widedata2)]<-0

#################################
#宽数据输出,传入第三个参数,即输出文件的前缀
output=as.character(args[3])
#output=as.character("bbb")
write.table(widedata1,paste(output,".v1.wide",sep=""),col.names =TRUE,row.names = FALSE,quote=F,sep = "\t") 
write.table(widedata2,paste(output,".v2.wide",sep=""),col.names =TRUE,row.names = FALSE,quote=F,sep = "\t") 

此模板以shell语言下经常用到的长数据变宽数据为例
输入数据 aaa,第一列必须为计数,二三列不特殊要求,不含列命

cat aaa
23 gene1 grape
24 gene2 grape
67 gene3 rice
88 gene2 rice
13 gene3 grape

run:

Rscript long2wide.R . aaa bbb

得到两个文件,以bbb为前缀

cat bbb.v1.wide
Group   gene1   gene2   gene3
grape   23      24      13
rice    0       88      67

cat bbb.v2.wide
Class   grape   rice
gene1   23      0
gene2   24      88
gene3   13      67

上一篇下一篇

猜你喜欢

热点阅读