简体   繁体   中英

Combine multiple text files into a single time series using R

This should be a simple task in r but I can't seem to get it right. I have multiple text files with single column and no header. I just want to combine them to have a single time series. Order doesn't matter. I tried following:

library(zoo)
t1 <- read.table("t1.txt") # 3,5,4,..... 
t2 <- read.table("t2.txt") # 5,6,0,.....
so on...
m <- merge(t1 = as.zoo(t1), t2 = as.zoo(t2))

I get:

1  3  5
2  5  6
3  4  0
.  .  .
.  .  . 

But I need it like following in single column:

3
5
4
.
.

Any suggestions?

Try

library(data.table)
files <- list.files(pattern="^t\\d+")
z1 <- as.zoo(rbindlist(lapply(files,fread)))

str(z1)
# ‘zoo’ series from 1 to 12
#  Data: int [1:12, 1] 3 5 1 5 7 9 3 4 9 7 ...
#- attr(*, "dimnames")=List of 2
# ..$ : NULL
#  ..$ : chr "Col1"
# Index:  int [1:12] 1 2 3 4 5 6 7 8 9 10 ...

Update

attr(z1, 'dimnames') <- NULL
attr(z1, 'index') <- NULL
str(z1)
#‘zoo’ series from  to 
#Data: int [1:12, 1] 3 5 1 5 7 9 3 4 9 7 ...
# Index:  NULL

 z1

#[1,] 3
#[2,] 5
#[3,] 1
#[4,] 5
#[5,] 7
#[6,] 9
#[7,] 3
#[8,] 4
#[9,] 9
#[10,] 7
#[11,] 7
#[12,] 9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM