#!/usr/bin/Rscript
# Usage: Rscript long2wide.R . input prefixofoutput
args<-commandArgs(TRUE)
#################################################
# R包加載,這里展示跨用戶/跨版本調(diào)用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")
#################################################
#工作目錄,傳入第一個參數(shù),即讀入與輸出數(shù)據(jù)所在的位置
setwd(args[1])
#setwd(".")
#################################################
#長數(shù)據(jù)的讀入,傳入第二個參數(shù),即特定格式的輸入數(shù)據(jù)
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
#################################
#寬數(shù)據(jù)輸出,傳入第三個參數(shù),即輸出文件的前綴
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語言下經(jīng)常用到的長數(shù)據(jù)變寬數(shù)據(jù)為例
輸入數(shù)據(jù) aaa,第一列必須為計數(shù),二三列不特殊要求,不含列命
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