-
Notifications
You must be signed in to change notification settings - Fork 0
/
transpose_data.R
executable file
·41 lines (35 loc) · 1.32 KB
/
transpose_data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env Rscript
# This script is a small function that transposes dataframes
# and outputs a new transposed file
# Script written by Chaochih Liu
# June 13, 2016
# To run the script: Rscript ./transpose_data.R <data.txt>
# This script assumes the 1st cell in the header contains text.
# Read in data and create data frame from it
# Transpose data
# Read in file without row.names = 1 to prevent manually shifting first row of cells over one cell
readFile <- function(filename) {
transposedData <- t(read.delim(file = filename,
header = FALSE))
return(transposedData)
}
# Write file to outfile
writeOutFile <- function(transposedData, filename) {
inputName <- unlist(strsplit(x = filename, split = ".txt"))
outputName <- paste(inputName, "transposed.txt", sep = "_")
write.table(x = transposedData,
file = outputName,
quote = FALSE,
sep = "\t",
eol = "\n",
col.names = FALSE,
row.names = FALSE)
}
# Run the script
main <- function() {
args <- commandArgs(trailingOnly = TRUE)
originalData <- args[1]
formatData <- readFile(originalData)
output <- writeOutFile(formatData, originalData)
}
main() # Run program