如何将列表写入R中的文件?
How to write a list into a file in R?
我需要帮助才能将列表打印到文件中。
这是列表:
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))
这个想法只是将该列表写入一个文件,例如 file1
列表需要在一行在两个()
之间,并且在内容之前加上诸如(因为我会一个接一个地写几个列表) :
预期输出文件:
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)
到目前为止,我尝试了:
sink("File1)
cat(paste0("Content_before1=",list1))
cat("\n")
cat(paste0("Content_before2=",list2))
sink()
(此示例包括两个列表)
我们可以使用 cat
和 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)
为了写入文件,指定 file
cat(sprintf('Content_before = (%s)\nContent_before2=(%s)',
toString(list1[[1]]), toString(list2[[1]])), file = 'file1.txt', '\n')
我需要帮助才能将列表打印到文件中。
这是列表:
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))
这个想法只是将该列表写入一个文件,例如 file1
列表需要在一行在两个()
之间,并且在内容之前加上诸如(因为我会一个接一个地写几个列表) :
预期输出文件:
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)
到目前为止,我尝试了:
sink("File1)
cat(paste0("Content_before1=",list1))
cat("\n")
cat(paste0("Content_before2=",list2))
sink()
(此示例包括两个列表)
我们可以使用 cat
和 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)
为了写入文件,指定 file
cat(sprintf('Content_before = (%s)\nContent_before2=(%s)',
toString(list1[[1]]), toString(list2[[1]])), file = 'file1.txt', '\n')