How to import a plain text file as a single character string in R?

845    Asked by Tanyaverma in Data Science , Asked on Jan 6, 2020
Answered by Tanya verma

Let us take and example of a text file

scan("example.txt", what="character", sep=NULL)

But this piece of code returns a vector but we want as a character string

For such case, we can use the readr library

install.packages("readr") # you only need to do this one time on your system library(readr)

srtingfile <- read_file("path/to/example.txt")

This will convert the text file into a string as we want


Your Answer

Interviews

Parent Categories