Skip to content
Advertisement

merge different files into 1 text file in R

I have two files with one being text, and the other being a data frame, now I just want to merge them into one as a text file. With linux, I can use:

cat file1 file2 > outputfile

I wonder if we can do the same thing with R?

file1

##TITLE=POOLED SAMPLES COLLECTED 05-06/03/2018
##JCAMP-DX=4.24
##DATA TYPE=LINK
#ORIGIN Bentley_FTS SomaCount_FCM 82048
##OWNER=Bentley Instruments Inc
##DATE=2018-03-09
##TIME=15:34:48
##BLOCKS=110
##LAB1=Auto Generated
##LAB2=
##CHANNELNAMES=8

file 2:

649.025085449219 0.063037105 0.021338785 -0.00053594 0.008937807 0.03266982
667.675231457819 0.028557044 0.005877694 -0.015043681 0.014945094 0.051547796
686.325377466418 0.021499421 0.017125281 0.043007832 0.04132269 0.027496092
704.975523475018 0.006128653 -0.014599532 -0.000335723 0.020189898 0.024547976
723.625669483618 0.018550962 0.018567896 0.014100821 0.013067127 0.027075281
742.275815492218 0.030145327 0.039745297 0.050556265 0.056621946 0.058416516
760.925961500818 0.040279277 0.01392867 -0.00143011 0.015103153 0.03580305
779.576107509418 0.031955898 0.013671243 0.000861743 0.000641993 0.001747168

Thanks alot Phuong

Advertisement

Answer

We can use file.append:

file.append("fileMerged.txt", "file1.txt")
file.append("fileMerged.txt", "file2.txt")

Or if files are already imported into R, then write with append:

#import to R
f1 <- readLines("file1.txt")
f2 <- readLines("file2.txt")

# output with append
write(f1, "fileMerged.txt")
write(f2, "fileMerged.txt", append = TRUE)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement