0

In this post it is explained how to write lines to a textfile:

Add lines to a file

Although it is not possible to use connection as file. For that reason it is not possible to run this code:

for (i in 1:10) 

{

    con <- file("en_US.twitter.txt", "r")
    line <- readLines(con, i)  
    write(line,file="D:/myfile.txt",append=TRUE)

next

}

Is there a way to overcome this?

Thank you!

user2165379
  • 317
  • 3
  • 17

1 Answers1

0

Do you just want to concatenate two files?

txt1 <- "Lorem ipsum dolor amet polaroid hot chicken pork belly, mlkshk commodo \
typewriter excepteur. Meditation mixtape edison bulb ad venmo duis mollit \
veniam poke voluptate celiac everyday carry vape hot chicken ea."

txt2 <- "Incididunt copper mug direct trade hella taxidermy. Occupy chia in \
laborum subway tile cornhole XOXO echo park green juice try-hard sustainable. \
Put a bird on it bicycle rights hell of non chillwave blog slow-carb shoreditch \
organic ad waistcoat stumptown qui.\n"

write(txt1, "1.txt")
write(txt2, "2.txt")

# append 1.txt to 2.txt
li <- readLines("1.txt")
write(li, "2.txt", append=TRUE)

cat(readLines("2.txt"), sep="\n")

If you just want to append the first n (in this case 2) lines

write(txt1, "1.txt")
write(txt2, "2.txt")

li <- readLines("1.txt", 2)
write(li, "2.txt", append=TRUE)

cat(readLines("2.txt"), sep="\n")

To append only specific line numbers (in this case 1 and 3)

write(txt1, "1.txt")
write(txt2, "2.txt")

li <- readLines("1.txt")
write(li[c(1, 3)], "2.txt", append=TRUE)

cat(readLines("2.txt"), sep="\n")
AkselA
  • 7,928
  • 2
  • 22
  • 33