How to read a txt file line by line in R/Rstudio?

前端 未结 3 1778
鱼传尺愫
鱼传尺愫 2021-01-21 04:26

Suppose I have a messy text file like the following for example

Today is a good
day, but I feel very tired. It seems like it is
going to rain pretty soon.


        
相关标签:
3条回答
  • 2021-01-21 04:34
    char_vector <- readLines(filename)
    
     txt <- "Today is a good
     day, but I feel very tired. It seems like it is
     going to rain pretty soon."
    
     readLines(textConnection(txt) )
    #   ---- teh screen output of three distinct character elements
    [1] "Today is a good"                                
    [2] "day, but I feel very tired. It seems like it is"
    [3] "going to rain pretty soon."                    
    
    char_vector <- readLines(textConnection(txt))
    
    char_vector[1]
    #[1] "Today is a good"
    
    0 讨论(0)
  • 2021-01-21 04:49

    scan can do this.

    scan( file="myfile.txt", what=character() ) 
    
    0 讨论(0)
  • 2021-01-21 05:01

    You can use readLines function.

    0 讨论(0)
提交回复
热议问题