带read.R表的向量
我想我可能把这件事想得太复杂了。我有一个文件要用于在R中绘制条形图。该文件如下所示:带read.R表的向量,r,io,plot,R,Io,Plot,我想我可能把这件事想得太复杂了。我有一个文件要用于在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 | | # | | | | | | |
## 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)
我想把我的数据做成一个条形图。我在想它会有点像这样:
## 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()
正如我在代码中所评论的,我得到的错误是“height”必须是向量或矩阵。我不太了解R,不知道如何把它作为向量读入,而知道我目前是如何做的。我也不确定问题是否在于我使用的是barplot。难道只是“情节”更适合使用吗?也许我使用read.table不正确 您可以使用?unlist将data.frame转换为数字向量:
d <- read.table(file="YOURFILE", sep="\t", header=TRUE, row.names=1)
barplot(unlist(d))
也许你得调整一下标签
顺便说一句:你可以避免你的一线和二线部分。read.table将跳过以开头的注释行和空行。有关详细信息,请参见?read.table。您能举个例子吗。否则,将无法调试。包括确切的输入文件,或其中的一部分,以及您正在使用的所有额外变量。。。您还可以加入dputfirstLine,这将非常有帮助。非常感谢。这实际上简化了我的代码。我没有看到readtable会跳过注释和空格。