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

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

问题描述

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

文件看起来像这样:

  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

除了每一行更长千分之一)。可以看出,所有的值都是非负整数。



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

我想读取所有的结果,然后对每个实验(行)做一些操作,比如计算ecdf。我也想计算所有复制品的平均ecdf。

我的问题 - 我应该怎么读这个奇怪的外观文件?我很习惯 read.table ,我不知道我曾经尝试过其他任何东西...我是否需要使用一些低级的
readlines方法?我猜首选输出将是一个列表(或向量?)的向量。我查看了 scan ,但似乎所有的矢量都必须是相同的长度。





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

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

似乎有效。它看起来不错?



当我 summary(results.list)我得到:长度类模式

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

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

解决方案

Josh链接的例子是我一直使用的。

  inputFile<  - /home/jal/myFile.txt
con< - file(inputFile, ()()())()()()()()()()()() ($($)$($)$($)$($)$($)$($) 1]]))
dataList< -c(dataList,myVector)

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

}

关闭(con)

我编辑了示例,从示例数据中创建两个列表。 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天全站免登陆