将.txt格式的混合表文件拆分为两个表文件

问题描述

在R编程语言中,是否可以像这样拆分.txt文件:mixed_data.txt->

enter image description here

在两个.txt文件中,例如: file1.txt

enter image description here

file2.txt

enter image description here

解决方法

以下是根据您的示例进行的尝试:

# 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")

该代码显然未经测试,因为我无法测试您发布的文件的图片。如果有问题,请将示例数据以文本形式而不是图片形式发布,否则我将无法调试。