简体   繁体   中英

cbind in time-series data

Here is a simple example that works fine:

a = 1; b = 2; c = 3
d = 65; e = 66; f = 77
m1 = cbind(a, b, c); m2 = cbind(d, e, f); M = cbind(m1, m2)
colnames(M)
#   [1] "a" "b" "c" "d" "e" "f"

But, now try exactly the same thing with time-series data:

a = 1; b = 2; c = 3
d = 65; e = 66; f = 77
m1 = as.ts(cbind(a, b, c)); m2 = as.ts(cbind(d, e, f)); M = cbind(m1, m2)
colnames(M)
#   [1] "m1.a" "m1.b" "m1.c" "m2.d" "m2.e" "m2.f"'

How do I avoid these prefixes for time series data?
(ie: the prefixes m1. and m2. )

PS: Obviously I know we can just do a direct "cbind" command on a, b, c, d, e, f bypassing m1 and m2 , but I need these intermediate staged matrices in a loop.

Can't explain why, but cbind.data.frame works the same for both:

a = 1; b = 2; c = 3
d = 65; e = 66; f = 77
m1 = cbind(a, b, c)
m2 = cbind(d, e, f)
M = cbind.data.frame(m1, m2)
colnames(M)
#[1] "a" "b" "c" "d" "e" "f"

m1 = as.ts(cbind(a, b, c))
m2 = as.ts(cbind(d, e, f))
M = cbind.data.frame(m1, m2)
colnames(M)
#[1] "a" "b" "c" "d" "e" "f"

There's no way to prevent cbind.ts from doing this. The usual way you would prevent it would be to set deparse.level=0 , but cbind.ts ignores it.

R> stats:::cbind.ts
function (..., deparse.level = 1) 
{
    if (deparse.level != 1) 
        .NotYetUsed("deparse.level != 1")
    .cbind.ts(list(...), .makeNamesTs(...), dframe = FALSE, union = TRUE)
}
<bytecode: 0x35531f8>
<environment: namespace:stats>

You can always set the colnames yourself, just be careful they are "valid" (eg via make.names ) and not duplicated, or you might have issues later in your analysis.

colnames(M) <- make.names(c(colnames(m1), colnames(m2)))

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