如何从R中的文本文件读取?

时间:2019-10-26 17:09:27

标签: r text

如何读取文本文件?我在文本文件中有以下数据-
 A,B,C,D
  E,F,G,H

我正在尝试以交互方式选择文件。

read.delim(file.choose(), sep=",")

Warning messages: 1: In read.table(file = file, header = header, sep = sep, quote = quote, : line 1 appears to contain embedded nulls<br> 2: In read.table(file = file, header = header, sep = sep, quote = quote,
: line 2 appears to contain embedded nulls<br> 3: In read.table(file = file, header = header, sep = sep, quote = quote,
: line 3 appears to contain embedded nulls<br> 4: In read.table(file = file, header = header, sep = sep, quote = quote,
: line 4 appears to contain embedded nulls<br> 5: In read.table(file = file, header = header, sep = sep, quote = quote,
: line 5 appears to contain embedded nulls<br> 6: In scan(file = file, what = what, sep = sep, quote = quote, dec = dec,
: EOF within quoted string<br> 7: In scan(file = file, what = what, sep = sep, quote = quote, dec = dec,
: embedded nul(s) found in input

我希望只读取数据并将其存储在变量中。

2 个答案:

答案 0 :(得分:0)

只需使用read.csv

your_df <- read.csv(file="/path/to/your_file.csv", header=FALSE)
your_df

  v1 v2 v3 v4
1  A  B  C  D
2  E  F  G  H

header的{​​{1}}参数告诉R您输入的CSV文件没有带有列名的前导标题行。

答案 1 :(得分:0)

安装并附加data.table,然后使用fread

fread(file.choose(), sep = ",")

您的错误可能是由于编码问题-指定正确的编码:

fread(file.choose(), sep = ",", encoding = "INSERT YOUR ENCODING"`)