简体   繁体   中英

write.table in R creates txt files that it then reads incorrectly

So I am working in MethylKit that has a function that necessitates the use of txt files.

My original files of my data work perfectly, but due to some other issues I am troubleshooting by creating subsets of my original files.

To do so I am using:

write.table(head(Sample_101, n=100),"shorty_101.txt", col.names = FALSE, 
sep = "\t", row.names = TRUE)

I was at first running into the issue of R shifting the column names and the above line works well for fixing that issue. However, when I attempt to put the txt files into the file.listshorty object that will then be used for the methRead function, I get this:

file.listshorty <- list("shorty_101.txt","shorty_102.txt", "shorty_103.txt", 
                    "shorty_104.txt", "shorty_105.txt", "shorty_107.txt")


myobjS = methRead(file.listshorty, sample.id=list("Sample_101_GDM", 
"Sample_102_GDM", "Sample_103_GDM", "Sample_104_nonGDM", 
"Sample_105_nonGDM", "Sample_107_nonGDM"), assembly = "hg19", 
treatment=c(1,1,1,0,0,0), context="CpG", dbtype = "tabix", pipeline = "amp", 
header= TRUE, skip = 0, sep = "\t", resolution = "base", dbdir = getwd(), 
mincov = 10)

Error in data[, 5] * data[, 6] : non-numeric argument to binary operator

However when I open these txt files created by R and remove the extraneous things added to it and then read them in, it works...

To clarify --> Removed the row numbering column and the row that names the columns V1, V2, V3...V7.

 V1 V2 V3 V4 V5 V6 V7
1
2
3
4
5

I have also tried to do:

write.table(head(Sample_101, n=100), "shorty_101.txt", col.names = FALSE, 
sep = "\t", row.names = FALSE)

It gives me the same error message. I am assuming it is because the first row needs to be denoted as the header but not sure how to do this using the list function or if there is another work around.

Any help is much appreciated!

Thanks!

Figured it out! You have to take out the quotes that are created in the txt file by adding the argument quote = FALSE in the write.table function.

write.table(head(Sample_101, n=100), "shorty_101.txt", col.names = FALSE, sep 
= "\t", row.names = FALSE, quote = FALSE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM