R语言读入文件然后输出文件
(2010-06-23 18:30:20)
标签:
r读入文件读出杂谈 |
分类: R语言学习 |
本人有一个文件,内容如下:
rs3094315 1 742429 0.3015
rs12562034 1 758311 0.7966
rs12124819 1 766409 0.2822
rs4475691 1 836671 0.0364
rs28705211 1 890368 0.0820
rs13303118 1 908247 0.8978
rs9777703 1 918699 0.0132
rs3121567 1 933331 0.1803
rs3934834 1 995669 0.2985
rs9442372 1 1008567 0.0649
rs3737728 1 1011278 0.6199
rs6687776 1 1020428 0.1092
rs9651273 1 1021403 1.7824
rs4970405 1 1038818 0.5863
命名为ex.txt,第四列是卡方值,我想把第四列转化为p值,用R写了以下程序:
> data<-read.table("ex.txt")
> for (i in 1:14) { result[i, 1] <- (1-pchisq(data[i, 4], 1))}; write.table (result, '1.txt')
但是老是提示:
错误于result[i, 1] <- (1 - pchisq(data[i, 4], 1)) : 找不到对象'result'
请高手赐教,谢谢!
解决办法:
>
> result<-data.frame();for (i in 1:14)
第二行:先声明一下result<-data.frame(); 然后就可以了,write.table是输出文件命令
问题2:
请教各位高人,怎么在write.table时,使输出的txt文件的数据全部列左对齐,列之间以多个空格隔离开,并去掉数据元素的引号?谢谢!
write.table(x,file='...',sep='\t',quote=F)
write.table {base}命令详解:
write.table {base} | R Documentation |
Data Output
Description
write.table
prints its
required argument x
(after converting it to a data
frame if it is not one already) to file
. The entries
in each line (row) are separated by the value of
sep
.
Usage
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ", eol = "\n", na = "NA", dec = ".", row.names = TRUE, col.names = TRUE, qmethod = c("escape", "double"))
Arguments
x |
the object to be
written, typically a data frame. If not, it is attempted to coerce
x to a data frame. |
file |
either a character
string naming a file or a connection. "" indicates
output to the console. |
append |
logical. If
TRUE , the output is appended to the file. If
FALSE , any existing file of the name is
destroyed. |
quote |
a logical value or a
numeric vector. If TRUE , any character or factor
columns will be surrounded by double quotes. If a numeric vector,
its elements are taken as the indices of the columns to quote. In
both cases, row and column names are quoted if they are written. If
FALSE , nothing is quoted. |
sep |
the field separator
string. Values within each row of x are separated by
this string. |
eol |
the character(s) to print at the end of each line (row). |
na |
the string to use for missing values in the data. |
dec |
the string to use for decimal points. |
row.names |
either a logical
value indicating whether the row names of x are to be
written along with x , or a character vector of row
names to be written. |
col.names |
either a logical
value indicating whether the column names of x are to
be written along with x , or a character vector of
column names to be written. |
qmethod |
a character string
specifying how to deal with embedded double quote characters when
quoting strings. Must be one of "escape" (default), in
which case the quote character is escaped in C style by a
backslash, or "double" , in which case it is doubled.
You can specify just the initial letter. |
Details
Normally there is no
column name for a column of row names. If col.names=NA
a blank column name is added. This can be used to write CSV files
for input to spreadsheets.
write.table
can be slow
for data frames with large numbers (hundreds or more) of columns:
this is inevitable as each column could be of a different class and
so must be handled separately. Function write.matrix
in package MASS may be much more efficient if
x
is a matrix or can be represented in a numeric
matrix.
See Also
The “R Data Import/Export” manual.
Examples
## Not run: ## To write a CSV file for input to Excel one might use write.table(x, file = "foo.csv", sep = ",", col.names = NA) ## and to read this file back into R one needs read.table("file.csv", header = TRUE, sep = ",", row.names=1) ## End(Not run)