R从多个CSV文件中提取单个数据 [英] R extract a single data from mutliple csv files

查看:279
本文介绍了R从多个CSV文件中提取单个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个csv文件(超过100个).每个文件代表一个时间段.在每个文件中,有29行需要跳过(文本行).在第30行,我有一个温度矩阵,它是纬度和经度坐标的函数.例如:在纬度68.80和经度48.40268时,温度为5.94.

I have multiple csv files (more than 100). Each file represents a time period. In each file, there are 29 lines that need to be skiped (text lines). At line 30, I have a matrix of temperature as a function of latitude and longitude coordinates. For example: at latitude 68.80 and longitude 48.40268, the temperature is 5.94.

因此,我想提取每个时间段(针对每个文件)在特定的经纬度坐标组合下的温度.

So, I would like to extract the temperature at a specific combination of latitude and longitude coordinates for every time period I have (for every file).

我可以为单个文件编写代码,但是我不知道该如何循环执行或如何使其更快.

I can write the code for a single file, but I'm affraid I don't know how to do it in a loop or how to make it faster.

感谢您的帮助,谢谢.抱歉,如果这与其他问题相似,我阅读了在该主题上可以找到的内容,但似乎不适合我的问题.

Any help is appreciated, thank you. Sorry if this is similar to other questions, I read what I could find on that topic, but it did not seem to fit for my problem.

一个文件的代码:

filenames <- list.files(path="E:/Documents...")
fileone <- read.csv(filenames[1], skip=29, header=T, sep=";")
names(fileone) <- c("Lat", "68.88", "68.86", "68.85", "68.83", "68.82", "68.80", "68.79", "68.77", "68.76", "68.74", "68.73", "68.71")
Tempone <- fileone[which(fileone$Lat==48.40268), "68.80"]

推荐答案

假定相对于您的系统的数据大小足够小(相对于您的系统) 一起放入内存,您可以使用列表一次完成此操作

Assuming data size relative to your system are small enough (relative to your system) to fit into memory all together, you can accomplish this in one shot using lists

## Grab the filienames, just like you're doing
filenames <- list.files(path="E:/Documents...")

## Assuming all columns have the same column names
c.nms <- c("Lat", "68.88", "68.86", "68.85", "68.83", "68.82", "68.80", "68.79", "68.77", "68.76", "68.74", "68.73", "68.71")


## Import them all in one shot, as a list of data.frames
AllData <- lapply(filenames, read.table, 
      skip=29, header=TRUE, sep=";", row.names=NULL, col.names=c.nms)

## Then to get all of your rows
PulledRows <- 
lapply(AllData, function(DF) 
    DF[fileone$Lat==48.40268, "68.80"]
  )

如果每个文件提取不同的纬度/经度,则可以将mapply与纬度/经度的向量/列表一起使用

If you are pulling different lat/longs per file, you can use mapply with a vector/list of lat/longs

这篇关于R从多个CSV文件中提取单个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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