I'm afraid it's a very rudimentary question, but I have a question about how to use loops to process multiple objects.
x1, x2, x3, ..., xn
When there is an xts object named
vx1,vx2,...,vxn
in an attempt to create multiple vectors called
i<-1
for (i in 1:n)
{
vx[i]<-as.vector(x[i])
i<-i+1
}
I tried, but it didn't work. Please let me know if there are any errors or misunderstandings in the code.
r
When you receive the question literally,
x1
and vx1
exist, which is not an array or a list.
You cannot access it as it is.
That's why I think it's necessary to make it into a list.For example, it looks like this.
require("xtts")
data(sample_matrix)
x1<-as.xtts(sample_matrix)
# is.xts (x1)
x2<-as.xtts (sample_matrix)
x3<-as.xtts(sample_matrix)
n<-3
xlst<-list (x1, x2, x3)
vx<-array(1:n)
for (i in 1:n) {
vx[i]<-as.vector(xlst[i])
}
For your information, 3100 says Here are some ways to "process on a vector basis."
Based on the code in the question, there is a repeatable array of x
, so let's do it all at once:
library(xtts)
data(sample_matrix)
Quoted from example.xts<-as.xts(sample_matrix,descr='my new xts object')#xts example
x<-c(sample.xtts, sample.xtts, sample.xtts)#xtts objects can also be inserted into vectors
vx<-sapply(x, as.vector)
Inspection of the output shows that there is no damage to the data itself (but I have no experience with xtts
, so please check it yourself):
>str(x)
An'xts' object on 2007-01-02/2007-06-30 containing:
Data: num [1:540, 1:4] 50 50 50 50.250.2 ...
- attr(*, "dimnames") = List of 2
..$ —NULL
..$ : chr[1:4] "Open" "High" "Low" "Close"
Indexed by objects of class: [POSIXct, POSIXt] TZ:
xtts Attributes:
List of 1
$ descr: chr "my new xts object"
>str(vx)
num [1:540, 1:4] 50 50 50 50.250.2 ...
- attr(*, "dimnames") = List of 2
..$ —NULL
..$ : chr[1:4] "Open" "High" "Low" "Close"
However, perhaps because c()
, the three xtts
objects (and the vx
column) that make up x
are now one.As a workaround, you can insert object configurations into list()
.
x<-list(sample.xtts, sample.xtts, sample.xtts)
vx<-lapply(x, as.vector)
Then,
>str(x)
List of 3
$ —An'xts' object on 2007-01-02/2007-06-30 containing:
Data: num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
- attr(*, "dimnames") = List of 2
..$ —NULL
..$ : chr[1:4] "Open" "High" "Low" "Close"
Indexed by objects of class: [POSIXct, POSIXt] TZ:
xtts Attributes:
List of 1
..$ descr: chr "my new xts object"
$ —An'xts' object on 2007-01-02/2007-06-30 containing:
Data: num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
- attr(*, "dimnames") = List of 2
..$ —NULL
..$ : chr[1:4] "Open" "High" "Low" "Close"
Indexed by objects of class: [POSIXct, POSIXt] TZ:
xtts Attributes:
List of 1
..$ descr: chr "my new xts object"
$ —An'xts' object on 2007-01-02/2007-06-30 containing:
Data: num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
- attr(*, "dimnames") = List of 2
..$ —NULL
..$ : chr[1:4] "Open" "High" "Low" "Close"
Indexed by objects of class: [POSIXct, POSIXt] TZ:
xtts Attributes:
List of 1
..$ descr: chr "my new xts object"
>str(vx)
List of 3
$ : num[1:720] 50 50.2 50.450.450.2 ...
$ : num[1:720] 50 50.2 50.450.450.2 ...
$ : num[1:720] 50 50.2 50.450.450.2 ...
The result is thatNow you have three vectors, but the data build information is missing.
As a compromise, you can call as.matrix
instead of as.vector
, but we're moving away from the first question, so let's wrap this up.Anyway, R's apply
system function is powerful.
For more information:
apply
About the family:http://d.hatena.ne.jp/a_bicky/20120425/1335312593mclapply
: http://mkprob.hatenablog.com/entry/2013/11/07/000227
Based on Ohga and Freedion's answers, I wonder if the following is true.
for (i in 1:n)
{
xn<-paste("x", i, sep="")
vxn<-paste("vx", i, sep="")
event(parse(text=paste(vxn, "<-array(1)"))))
event(parse(text=paste(vxn, "<-as.vector(",xn,"))))
}
I don't know what I want to do, so I'm not confident.
First, you do not need i<-i+1
.(Was it that level of question?)
x1
cannot be represented as x[1]
(different), so for example, in the first stage of the loop,
x<-c (NULL, x1, x2, x3, ..., xn)
So, while preparing x, put x1
in x[1]
and x2
in x[2]
.
(In this question, x[0]
, x0
is not used, so I added NULL.)
Next, also in the first stage of the loop...
vx<-c()
has variables to accept as.vector(x[i]).
In , I was asked a question
i<-1
for (i in 1:n)
{
vx[i]<-as.vector(x[i])
// i<-i+1
}
First, vx[1], vx[2], vx[3], ...vx[n]
have values.
However, it is not in vx1,vx2,vx3,...vxn
.If you want to do this, you can do it steadily even after the loop is over.
vx1<-vx[1]
vx2<-vx[2]
vx3<-vx[3]
...
vxn<-vx[n]
I don't think I'm going to do that. (Maybe there's something else.
If you think it's just the literal meaning of the question,
for (i in 1:n) {
assign(paste('vx', i, sep='), get(paste('x', i, sep=')))
}
can generate vx1,vx2,...,vxn
.
assign('A', a)
allows you to assign a
to variables A
get('A')
to take the stored value by specifying the variable name as a string.Note that both are dynamic and therefore performance is fairly slow.Also, these methods are like behind-the-scenes techniques when the specifications required are unavoidable, not the usual R
style.
Instead, R
works better on a vector-by-vector basis, so we recommend doing so.
© 2024 OneMinuteCode. All rights reserved.