在 R 中逐行阅读的好方法是什么? [英] What is a good way to read line-by-line in R?

查看:19
本文介绍了在 R 中逐行阅读的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中每一行都是在实验的特定重复中收集的一组结果.每个实验中的结果数量(即每行中的列数)可能不同.每行中的结果顺序也不重要(第 1 行中的第一个结果和第一个结果 2 的相关性不比任何其他对更相关;这些是结果的集合).>

文件看起来像这样:

2141 0 5328 5180 357 5335 1 5453 5325 5226 7 4880 5486 02650 0 5280 4980 5243 5301 4244 5106 5228 5068 5448 3915 4971 5585 4818 4388 5497 4914 5364 48049 32069 2595 2478 49412627 3319 5192 5106 32 4666 3999 5503 5085 4855 4135 4383 47702005 2117 2803 2722 2281 2248 2580 2697 2897 4417 4094 4722 5138 5004 4551 5758 5468 173611914 1977 2414 100 2711 2171 3041 5561 4870 4281 4691 4461 5298 3849 5166 5578 5520 4634 4856 500 1952539 2326 0 4617 3735 0 5122 5439 5238 125 5316 21173 4492 5038 5944 5576 5424 5139 5184 5 5096 4963 2771 2808 2592 24963 9428 17152 5467 5202 6038 5094 5221 5469 5079 3753 5080 5141 4097 5173 11338 4693 5273 5103 513 42024 2 2822 5097 5239 5296 4561

除了每一行都长得多(最多几千个值).可以看出,所有值都是非负整数.

简而言之——这不是一个普通的表格,其中的列是有意义的.它只是一堆结果 - 每一个都排成一行.

我想读取所有结果,然后对每个实验(行)进行一些操作,例如计算ecdf.我还想计算所有重复的平均 ecdf.

我的问题 - 我应该如何阅读这个奇怪的文件?我太习惯 read.table 以至于我不确定我是否尝试过其他任何东西......我是否必须使用一些低级的阅读行?我猜首选输出将是向量列表(或向量?).我查看了 scan 但似乎所有向量都必须具有相同的长度.

任何建议将不胜感激.

更新按照下面的建议,我现在做这样的事情:

con <- file('myfile')打开(con);results.list <- list();current.line <- 1while (length(line <- readLines(con, n = 1, warn = FALSE)) > 0) {results.list[[current.line]] <- as.integer(unlist(strsplit(line, split="")))current.line <- current.line + 1}关闭(骗局)

似乎有效.看起来好吗?

当我 summary(results.list) 我得到:Length Class Mode

 长度分类模式[1,] 1091 -none- 数字[2,] 1070 -none- 数字....

类不应该是整数吗?什么是模式?

解决方案

Josh 链接的示例是我一直使用的示例.

inputFile <- "/home/jal/myFile.txt"con <-文件(输入文件,打开=r")数据列表 <- 列表()ecdfList <- list()while (length(oneLine <- readLines(con, n = 1, warn = FALSE)) > 0) {myVector <- (strsplit(oneLine, " "))myVector <- list(as.numeric(myVector[[1]]))dataList <- c(dataList,myVector)myEcdf <- ecdf(myVector[[1]])ecdfList <- c(ecdfList,myEcdf)}关闭(骗局)

我编辑了示例以根据您的示例数据创建两个列表.dataList 是一个列表,其中列表中的每一项都是文本文件中每一行的数值向量.ecdfList 是一个列表,其中每个元素都是文本文件中每一行的 ecdf.

您可能应该在其中添加一些 try() 或 trycatch() 逻辑,以正确处理由于空值等原因而无法创建 ecdf 的情况.但是上面的例子应该会让你非常接近.祝你好运!

I have a file where each line is a set of results collected in specific replicate of an experiment. The number of results in each experiment (i.e. number of columns in each row) may differ. There's also no importance to the order of the results in each row (the first result in row 1 and the first result 2 are not more related than any other pair; these are sets of results).

The file looks something like this:

2141 0 5328 5180 357 5335 1 5453 5325 5226 7 4880 5486 0 
2650 0 5280 4980 5243 5301 4244 5106 5228 5068 5448 3915 4971 5585 4818 4388 5497 4914 5364 4849 4820 4370
2069 2595 2478 4941 
2627 3319 5192 5106 32 4666 3999 5503 5085 4855 4135 4383 4770 
2005 2117 2803 2722 2281 2248 2580 2697 2897 4417 4094 4722 5138 5004 4551 5758 5468 17361 
1914 1977 2414 100 2711 2171 3041 5561 4870 4281 4691 4461 5298 3849 5166 5578 5520 4634 4836 4905 5105 5089
2539 2326 0 4617 3735 0 5122 5439 5238 1
25 5316 21173 4492 5038 5944 5576 5424 5139 5184 5 5096 4963 2771 2808 2592 2
4963 9428 17152 5467 5202 6038 5094 5221 5469 5079 3753 5080 5141 4097 5173 11338 4693 5273 5283 5110 4503 51
2024 2 2822 5097 5239 5296 4561 

except each line is much longer (up to a few thousand values). As can be seen, all values are non-negative integers.

To put it short - this is not a normal table, where the columns have meanings. Its just a bunch of results - each set in a line.

I would like to read all the results, then do some operations on each experiment (row), such as calculating the ecdf. I would also like to calculate the average ecdf over all the replicates.

My problem - how should I read this strange looking file? I'm so use to read.table that I'm not sure I ever tried anything else... Do I have to use some low-level like readlines? I guess the preferred output would be a list (or vector?) of vectors. I looked at scan but it seems all vectors must be of the same length there.

Any suggestions will be appreciated.

UPDATE Following the suggestions below, I now do something like this:

con <- file('myfile') 
open(con);
results.list <- list();
current.line <- 1
while (length(line <- readLines(con, n = 1, warn = FALSE)) > 0) {
 results.list[[current.line]] <- as.integer(unlist(strsplit(line, split=" ")))
 current.line <- current.line + 1
} 
close(con)

Seems to work. Does it looks OK?

When I summary(results.list) I get:Length Class Mode

      Length Class  Mode  
 [1,] 1091   -none- numeric
 [2,] 1070   -none- numeric
   ....

Shouldn't the class be integer? And what is the mode?

解决方案

The example Josh linked to is one that I use all the time.

inputFile <- "/home/jal/myFile.txt"
con  <- file(inputFile, open = "r")

dataList <- list()
ecdfList <- list()

while (length(oneLine <- readLines(con, n = 1, warn = FALSE)) > 0) {
    myVector <- (strsplit(oneLine, " "))
    myVector <- list(as.numeric(myVector[[1]]))
    dataList <- c(dataList,myVector)

    myEcdf <- ecdf(myVector[[1]])
    ecdfList <- c(ecdfList,myEcdf)

  } 

close(con)

I edited the example to create two lists from your example data. dataList is a list where each item in the list is a vector of numeric values from each line in your text file. ecdfList is a list where each element is an ecdf for each line in your text file.

You should probably add some try() or trycatch() logic in there to properly handle situations where the ecdf can't be created because of nulls or some such. But the above example should get you pretty close. Good luck!

这篇关于在 R 中逐行阅读的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆