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
  • 445
  • 4
  • 20

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
  • 8,153
  • 2
  • 21
  • 34
  • My endgoal is to copy specific linenumbers from the file twitter.txt to myfile.txt. This in order to make a subsample from twitter.txt. – user2165379 May 27 '18 at 17:55
  • The output from `readLines()` is just a character vector. You can select the elements as you would with any other vector. – AkselA May 27 '18 at 18:01
  • What kind of other vector for example? – user2165379 May 27 '18 at 18:05
  • See [here](https://www.statmethods.net/input/datatypes.html) for an introduction to R data types, and [here](http://adv-r.had.co.nz/Data-structures.html) for a more thorough discussion. – AkselA May 27 '18 at 18:08
  • thanks, i will try to integrate your answer in my code. Although i think the problem is in using connection as file. (See also the original post) – user2165379 May 27 '18 at 19:19
  • @user2165379: Is it absolutely necessary to use `connection()`? – AkselA May 27 '18 at 19:24
  • I have tried to put the connection outside of the loop and close it, although this gives a long error. setwd("D:/coursera/Capstone/Coursera-SwiftKey/final/en_US") con <- file("en_US.twitter.txt", "r") allelines <- readLines(con) close(con) for (i in 1:10) {print(i) myline <- readLines(allelines, i) write(myline,file="D:/coursera/Capstone/Coursera-SwiftKey/final/en_US/myfile.txt",append=TRUE) next } – user2165379 May 27 '18 at 19:45
  • What I mean is that this whole `con <- file("en_US.twitter.txt", "r"); allelines <- readLines(con); close(con)` dance seems unnecessary. You can use `readLines()` and `write()` without all that. Like I did in my answer above. Just change `"1.txt"` etc. to the path of your files. – AkselA May 27 '18 at 19:54
  • that would be great. I will try tomorrow and let you know! Thanks for your help! – user2165379 May 27 '18 at 19:56
  • That is the solution! I runs fine now without (con) as you mentioned.Thanks a lot! – user2165379 May 28 '18 at 14:04
  • @user2165379: Good to hear – AkselA May 28 '18 at 14:17