簡體   English   中英

按月聚合(匯總)多個時間序列數據 in.r

[英]Aggregate (Summarize) multiple Time Series Data by Month in .r

我有數百個每日天氣數據,擴展名為.txt,常用文件夾中以逗號 (",") 作為分隔符。 每個文件具有相同的數據結構,但文件名不同。 下面是一個數據結構的例子:

$ year         : int  1980 1980 1980 1980 1980 1980 1980 1980 1980 1980 ...
$ month        : int  1 1 1 1 1 1 1 1 1 1 ...
$ day          : int  1 2 3 4 5 6 7 8 9 10 ...
$ V1           : num  18.4 22.9 19.9 22.9 23.4 9.8 13.9 17.5 20.3 22.7 ...
$ V2           : num  30.8 31.5 31.4 31.3 31.5 29.8 30.1 30.6 30.5 31.1 ...
$ V3           : num  23.4 23.7 23.2 23.3 23.4 22.9 23 23.4 23.1 23.2 ...
$ V4           : num  2.2 0 0 0 0.9 3.6 3.5 3.7 1.2 0 ...
$ V5           : num  0.93 0.86 0.88 0.87 0.87 0.98 1 0.96 0.96 0.91 ...
$ V6           : num  1.6 3.5 5.2 5.5 3.9 4.2 4.2 4.9 4.9 4.4 ...

我需要對每個文件中的其中一個變量(比如說 V4)的總月度進行總結。 而想要的每個文件的output數據結構是這樣的(第一列是年,第二列是月,第三列是V4日值的總和):

Year 1  Month 1 22.1
Year 1  Month 2 82.4
Year 1  Month 3 142.8
Year 1  Month …etc  314
Year 2  Month 1 48.9
Year 2  Month 2 173.6
Year 2  Month 3 76.2
Year 2  Month …etc  517.4
Year 3  Month 1 117.8
Year 3  Month 2 20.1
Year 3  Month 3 169.8
Year 3  Month …etc  191.5

然后我需要將結果導出為所有文件中的 unique.txt 文件,新文件的名稱根據每個文件的原始文件(例如:before_file1.txt 到 result_file1.txt)。 我有一個使用 Purrr 的腳本,但似乎什么也沒發生。 如果您願意用正確的方法幫助我改進腳本,請多多指教。 謝謝

# Load packages
library(tidyverse)
library(dplyr)
library(purrr)

# Setting working directory
workingdirectory <- "D:/Directory"
setwd(workingdirectory)

# Listing the files in the folder with .txt extension
FilesList <- list.files(workingdirectory, pattern = "\\.txt$", full.names = TRUE)

# Looping per files
purrr::map(FilesList, ~{
 .x %>%
    # Read csv file
    read.csv(sep = ",", header = FALSE, stringsAsFactors = FALSE) %>% 
    # select variables 
    variables <- c("year", "month", "day", "V4") %>% 
    # summarize monthly of V4
    group_by(month, year) %>% 
    summarise(monthly = sum(V4)) %>% 

})

# Write the data back
write.csv(paste0('Result_', basename(.x)), sep = ",", row.names = FALSE)

我已經編輯了腳本,但是有一個錯誤。 請幫助修復它。 謝謝

Error: unexpected '}' in:
"    
}"
> 
> # Write the data back
> write.csv(paste0('TM_', basename(.x)), sep = ",", row.names = FALSE)
Error in basename(.x) : object '.x' not found
In addition: Warning message:
In write.csv(paste0("TM_", basename(.x)), sep = ",", row.names = FALSE) :
  attempt to set 'sep' ignored

我認為你已經在正確的方向。 我建議的解決方法是在運行 purrr::map function 之前定義 function。

因此,代碼應如下所示:

# Load packages
library(tidyverse)
library(dplyr)
library(purrr)

# Setting working directory
workingdirectory <- "D:/Directory"
setwd(workingdirectory)

# Listing the files in the folder with .txt extension
FilesList <- list.files(workingdirectory, pattern = "\\.txt$", full.names = TRUE)
columnNames <- c("year", "month", "day", "pcp_day")
# define function
processing <- function(x){
  x %>% read.csv(sep = "", header = FALSE, stringsAsFactors = FALSE) %>% rename_at(c(1,2,3,7), ~columnNames) %>% filter(month != 2 | day != 29) %>% group_by(month, year) %>% summarise(monthly = sum(pcp_day))
}
# Looping per files and # Write the data back
purrr::map(FilesList, ~processing(.x) %>% write.csv(paste0('Result_', basename(.x)), row.names = FALSE))

如果運行成功,您可以在您工作的工作目錄中找到輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM