如何将带有换行符的数据从文本文件导入R? [英] How to import data with line breaks from text file into R?
本文介绍了如何将带有换行符的数据从文本文件导入R?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个要导入到R
的文本文件.问题是,文件看起来像这样:
I have a text file that I'd like to import into R
. Problem is, the file looks like this:
x1,x2,x3,x4,x5,x6,x7,x8,x9,10,x11
1953.00 7.40000 159565. 16.6680 8883.00
47.2000 26.7000 16.8000 37.7000 29.7000
19.4000
1954.00 7.80000 162391. 17.0290 8685.00
46.5000 22.7000 18.0000 36.8000 29.7000
20.0000
以此类推.
我尝试了> data <- read.table("clipboard", header=TRUE)
,但是没有用.
I tried > data <- read.table("clipboard", header=TRUE)
but that didn't work.
推荐答案
虽然数据格式不正确,但在以下假设条件下仍可以对其进行解析:
While the data is ill-formed it still can be parsed given the following assumptions:
- 标题定义了多少个变量(结果表中的列)
- 数据本身是完整的-例如没有遗漏的值
- 数据属于统一类型(例如
numeric()
)
以下是用于解析所提供的示例数据的代码,就像从名为data.txt
的文本文件中读取该数据一样:
The following is code that parses the provided sample data as if it were read in from a text file called data.txt
:
# read in the header and split on ","
header = strsplit(readLines('data.txt', n=1), ',')[[1]]
# the length of the header determines how many variables there are
# read in the data which appears to have the pattern
# <numbers><whitespace><numbers>...
# skipping the first line since it was already parsed as the header
data = scan('data.txt', skip=1, what=numeric())
# reform the data (which is read in as a 1D numeric vector) into a 2D matrix
# with the same number of columns as there are headers (filling by rows).
# header names are assigned via the `dimnames=` argument
data = matrix(data, ncol=length(header), byrow=T, dimnames=list(NULL, header))
产生以下输出:
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11
[1,] 1953 7.4 159565 16.668 8883 47.2 26.7 16.8 37.7 29.7 19.4
[2,] 1954 7.8 162391 17.029 8685 46.5 22.7 18.0 36.8 29.7 20.0
这篇关于如何将带有换行符的数据从文本文件导入R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文