Angelo
Angelo

Reputation: 5059

Saving entire variable in file - including names

This could be a very dumb question, but I have been struggling with this one.

I have a certain values store in variable in R.The variable is say mycut

when I want to see what is in my variable mycut, I see

In R

>head (mycut)
chr1;884869;884870 chr1;1022900;1022901 chr1;1052886;1052887
               1                    1                    2
chr1;1052949;1052950 chr1;1093940;1093941 chr1;1102389;1102390
               2                    2                    1

but when i write this variable mycut to a file

>write(mycut, file='mycut')

and then I see the value I get only

1  1  2
2  2  1

I am missing the rest of the information. I would ideally like it in this format

chr1;884869;884870   1
chr1;1022900;1022901 1
chr1;1052886;1052887 2

tab seperated.

How can I do this.

Please help.

Thank you

Upvotes: 1

Views: 41

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70633

What you have is a vector with names. You can turn it into a data.frame and write it using write.table.

> x <- c(sausage1 = 1, sausage2 = 2, sausage3 = 2, sausage4 = 2)
> x
sausage1 sausage2 sausage3 sausage4 
       1        2        2        2 
> write.table(as.data.frame(x), row.names = TRUE, col.names = FALSE, file = "temp.txt", quote = FALSE)
> system("cat temp.txt")
sausage1 1
sausage2 2
sausage3 2
sausage4 2

Upvotes: 5

Related Questions