列表和矩阵使用sapply [英] Lists and matrix using sapply

查看:165
本文介绍了列表和矩阵使用sapply的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的问题,并且已经在网上搜索过.我在读取文件时遇到问题.但是,我按照@Konrad的建议设法读取了文件,我对此表示赞赏:如何使R从一个大目录下的多个子目录中读取文件?

I have a perhaps basic questions and I have searched on the web. I have a problem reading files. Though, I managed to get to read my files, following @Konrad suggestions, which I appreciate: How to get R to read in files from multiple subdirectories under one large directory?

这是一个类似的问题,但是,我还没有解决.

It is a similar problem, however, I have not resolved it.

我的问题:

我在不同的文件夹中有大量具有相同名称的文件("tempo.out").此tempo.out具有5列/标题.它们都是相同的格式,有1048行和5列:

I have large number of files of with same name ("tempo.out") in different folders. This tempo.out has 5 columns/headers. And they are all the same format with 1048 lines and 5 columns:

id X Y时间温度

id X Y time temp

setwd("~/Documents/ewat")
dat.files  <- list.files(path="./ress",
                 recursive=T,
                 pattern="tempo.out"
                 ,full.names=T)
readDatFile <- function(f) {
dat.fl <- read.table(f)  
 }

data.filesf <- sapply(dat.files, readDatFile)                         

# I might not have the right sintax in sub5:
subs5 <- sapply(data.filesf,`[`,5) 
matr5 <- do.call(rbind, subs5)   

probs <- c(0.05,0.1,0.16,0.25,0.5,0.75,0.84,0.90,0.95,0.99)
q <- rowQuantiles(matr5, probs=probs)
print(q)

我想提取成千上万个文件的第五列(临时)并进行分位数之类的计算.

I want to extract the fifth column (temp) of each of those thousands of files and make calculations such as quantiles.

我首先尝试读取"ress"中的所有子文件

I tried first to read all subfiles in "ress"

后者没有错误,但是我的主要问题是"data.filesf"不是矩阵而是列表,实际上第5列不是我期望的.然后是以下内容:

The latter gave no error, but my main problem is the "data.filesf" is not a matrix but list, and actually the 5th column is not what I expected. Then the following:

matr5 <- do.call(rbind, subs5)

也未提供所需的值/结果.

is also not giving the required values/results.

将列变成将成为巨大矩阵的最佳方法是什么?

What could be the best way to get columns into what will become a huge matrix?

推荐答案

考虑扩展定义的函数 readDatFile ,以提取第五列 temp 并直接分配给sapplyvapply的矩阵(因为您预先知道所需的结构-等于nrows或1048的数字矩阵长度).然后,运行所需的rowQuantiles:

Consider extending your defined function, readDatFile, to extract fifth column, temp, and assign directly to matrix with sapply or vapply (since you know ahead the needed structure -numeric matrix length equal to nrows or 1048). Then, run needed rowQuantiles:

setwd("~/Documents/ewat")

dat.files  <- list.files(path="./ress",
                         recursive=T,
                         pattern="tempo.out",
                         full.names=T)

readDatFile <- function(f) read.table(f)$temp  # OR USE read.csv(f)[[5]]

matr5 <- sapply(dat.files, readDatFile, USE.NAMES=FALSE)                         
# matr5 <- vapply(dat.files, readDatFile, numeric(1048), USE.NAMES=FALSE)

probs <- c(0.05,0.1,0.16,0.25,0.5,0.75,0.84,0.90,0.95,0.99)
q <- rowQuantiles(matr5, probs=probs)

这篇关于列表和矩阵使用sapply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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