在 R 编程语言中,是否可以像这样拆分 .txt 文件:mixed_data.txt ->
在两个 .txt 文件中,例如: file1.txt
和file2.txt
这是基于您的示例的尝试:
# read in the original file
x = readLines("example_mixed_dataset.txt")
# break it into two pieces based on the first occurrence of the word "Index"
# and write to two separate files
index = grep("Index", x)
writeLines(x[1:(index - 1)], "file1.txt")
writeLines(x[index:length(x)], "file2.txt")
此代码显然未经测试,因为我无法在您发布的文件图片上进行测试。如果有问题,请将示例数据以文本形式发布,不要以图片形式发布,否则无法调试。