Reputation: 868
I have a dataset ped
which I want to write into a tab separated file. However the header needs to start with a hash (#
) sign (due to the demands of the program who should read the file. How do I put the hash sign in there?
ped <- ped [,c('FAM_ID','IND_ID','FAT_ID','MOT_ID','SEX','DISEASE','RR','AGE')]
setwd(proj.dir)
write.table(ped, "DB/asymmetry.ped", sep="\t", row.names = FALSE, quote = FALSE)
Upvotes: 1
Views: 1066
Reputation: 73295
You might try:
cat("#", file = "DB/asymmetry.ped")
write.table(ped, "DB/asymmetry.ped", sep="\t", row.names = FALSE, quote = FALSE,
append = TRUE)
Note, you will get a warning message from write.table
as this is the default behaviour:
## somewhere inside `write.table`:
if (!is.null(col.names)) {
if (append)
warning("appending column names to file")
but it causes no harm.
Another possible way, is to append an "#"
to your first column name, then use write.table
as usual (with quote = FALSE
):
ped <- ped [,c('FAM_ID','IND_ID','FAT_ID','MOT_ID','SEX','DISEASE','RR','AGE')]
NAMES <- names(ped)
NAMES[1] <- paste0("#", NAMES[1])
setwd(proj.dir)
write.table(ped, "DB/asymmetry.ped", sep="\t", row.names = FALSE, quote = FALSE,
col.names = NAMES)
I would recommend this way. Let's have a test:
x <- trees[1:3, ] ## use built-in dataset `trees`
NAMES <- names(x)
NAMES[1] <- paste0("#", NAMES[1])
## write to screen (stdout) for inspection
write.table(x, row.names = FALSE, col.names = NAMES, quote = FALSE)
#Girth Height Volume
8.3 70 10.3
8.6 65 10.3
8.8 63 10.2
In case you want to add an #
at the start of every line / row, you can append a "#"
column as the first column:
ped <- cbind.data.frame("#" = "#", ped[,c('FAM_ID','IND_ID','FAT_ID','MOT_ID','SEX','DISEASE','RR','AGE')])
setwd(proj.dir)
write.table(ped, "DB/asymmetry.ped", sep="\t", row.names = FALSE, quote = FALSE)
Again, let's have a test:
x <- cbind.data.frame("#" = "#", trees[1:3, ])
## write to screen (stdout) for inspection
write.table(x, row.names = FALSE, quote = FALSE)
# Girth Height Volume
# 8.3 70 10.3
# 8.6 65 10.3
# 8.8 63 10.2
Upvotes: 3