如何在 R/Rstudio 中逐行读取 txt 文件? [英] How to read a txt file line by line in R/Rstudio?
本文介绍了如何在 R/Rstudio 中逐行读取 txt 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个像下面这样的乱七八糟的文本文件
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.
我想将 txt 文件作为字符向量读入 R 中,与它在原始文本文件中的显示方式完全相同.换句话说,我想让字符向量的第一个元素是第一行,字符向量的第二个元素是第二行,依此类推,如下
I want to read the txt file into R as a character vector exactly the way it appears as in the original text file. In other words, I want to have the first element of the character vector being the first line, second element of the character vector being the second line, etc, as below
char_vector[1] = "Today is a good"
char_vector[2] = "day, but I feel very tired. It seems like it is"
char_vector[3] = "going to rain pretty soon."
我尝试过 read.table 和 read.csv,但总是会出现一些行合并为一行的情况.有没有办法解决这个问题?
I have tried read.table and read.csv, but it's always the case that some lines join together as one line. Is there a way to fix this?
推荐答案
您可以使用 readLines
函数.
这篇关于如何在 R/Rstudio 中逐行读取 txt 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文