chippycentra
chippycentra

Reputation: 3432

How to write a list into a file in R?

I need help in order to print a list into a file.

Here is the list:

   list1<- list(c(1, 2, 3, 4, 5, 6, 14, 15, 28, 29, 30, 35, 36, 49, 50, 
    73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 
    89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 
    104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 
    117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 
    130, 131, 132, 133, 134, 150, 162, 163, 167, 172, 177, 199, 200, 
    201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 
    214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 
    227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 
    240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251))

list2<-list(c(12,367,78))

and the idea is simply to write that list into a file such as file1

The list needs to be in one line between two () and with a content juste before such as (because I will write several list one after each other):

Expected outputfile:

Content_before=(1, 2, 3, 4, 5, 6, 14, 15, 28, 29, 30, 35, 36, 49, 50, 
    73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 
    89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 
    104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 
    117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 
    130, 131, 132, 133, 134, 150, 162, 163, 167, 172, 177, 199, 200, 
    201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 
    214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 
    227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 
    240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251)
 Content_before2=(12,367,78)

So far, I tried:

sink("File1)
cat(paste0("Content_before1=",list1))
cat("\n")
cat(paste0("Content_before2=",list2))
sink()

(this example includes two lists)

Upvotes: 1

Views: 58

Answers (1)

akrun
akrun

Reputation: 886938

We can use cat with sprintf

cat(sprintf('Content_before = (%s)\nContent_before2=(%s)', 
     toString(list1[[1]]), toString(list2[[1]])), '\n')
#Content_before = (1, 2, 3, 4, 5, 6, 14, 15, 28, 29, 30, 35, 36, 49, 50, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 150, 162, 163, 167, 172, 177, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251)
#Content_before2=(12, 367, 78) 

Inorder to write to a file, specify the file

cat(sprintf('Content_before = (%s)\nContent_before2=(%s)', 
     toString(list1[[1]]), toString(list2[[1]])), file = 'file1.txt', '\n')

Upvotes: 1

Related Questions