2013-07-30 47 views
1

我想我可能会在我的脑海里过于复杂。我有我想要使用,使在R的barplot文件看起来是这样的文件:从与read.table R的向量R

## metadata 
# filepath 
## filename 
# Started on: Tue Jul 30 10:46:57 EDT 2013 


#HISTOGRAM java.lang.Integer 
READ: 1 2 
-1 28 28 
0 27 29 

我想让我的数据的条形图。我认为它看起来有点像这样:

| 
    | 
# |    | 
    | |   | 
    | |  | | 
    |__|_____|____|___|____ 
    -1(1) -1(2) 0(1) 0(2) 

但是我在阅读数据时遇到了问题。这是我的代码:

# Parse the arguments 
args <- commandArgs(trailing=T) 
#Chart file 
metricsFile <- args[1] 
#pdf file path and name that I want to produce 
outputFile <- args[2] 

# Figure out where the firstLine and the chart are in the file and parse them out 
startFinder <- scan(metricsFile, what="character", sep="\n", quiet=TRUE, blank.lines.skip=FALSE) 
firstBlankLine=0 
#finds empty strings 
for (i in 1:length(startFinder)) 
{ 
     if (startFinder[i] == "") { 
       if (firstBlankLine==0) { 
         firstBlankLine=i+1 
       } else { 
         secondBlankLine=i+1 
         break 
       } 
     } 
} 

#I accept some args and I read past some header information 
firstLine <- read.table(metricsFile, header=T, nrows=1, sep="\t", skip=firstBlankLine) 
#prints "firstLine: -1" "firstLine: 28" "firstLine: 28" 
secondLine <- read.table(metricsFile, header=T, nrows=1, sep="\t", skip=secondBlankLine) 
#prints "secondLine: -1" "secondLine: 27" "secondLine: 29" 

# Then plot as a PDF 
pdf(outputFile) 
#I am just inputing firstLine here because I was trying to see how it works 
#I get the error:'height' must be a vector or a matrix 
barplot(firstLine, 
     main=paste("Read",(i-1)," Distribution ", 
     xlab="Counts", 
     ylab="F/R Orientation") 

dev.off() 

正如我在我的代码中评论我得到的错误,'高度'必须是一个向量或矩阵。我不知道R是否足够了解如何以矢量的形式读取它,而不是我目前正在做什么。我也不确定是否问题是我正在使用barplot。难道只是'阴谋'更好用吗?也许我错误地使用了read.table?

+0

你可以让你的例子[reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。否则,将无法进行调试。包括确切的输入文件或其中的一部分以及所使用的所有额外变量......您还可以包含'dput(firstLine)',这将对您有所帮助。 – Justin

回答

1

你可以使用?unlist “转换” 你data.frame为数字矢量:

d <- read.table(file="YOURFILE", sep="\t", header=TRUE, row.names=1) 
barplot(unlist(d)) 

enter image description here

也许你需要调整的标签。

顺便说一句:你可以避开你的firstlinesecondline部分。 read.table将自动跳过评论(以#开头的行)和空行(详情请参阅?read.table)。

+0

非常感谢。这实际上很简化了我的代码。我没有看到该阅读表会跳过评论和空白。 – Stephopolis