单个命令有效时,R循环不起作用

时间:2019-05-25 23:32:55

标签: r

我试图将许多时间序列xts对象隐瞒,而我编写的for循环无法正常工作,我不知道为什么。

这不仅会发生在此特定任务上,还会发生在我执行的其他任务上,我有一个名为“代码”的列表,其中包含要转换的所有xts对象的名称列表。

import turtle
from PIL import Image
kel = turtle.Turtle()

class Draw_pattern(object):
    def __init__(self,posx,posy,size):
        self.posx = posx
        self.posy = posy
        self.size = size

    def draw_grid(self,initx, inity):
        # Drawing grid
        kel.penup()
        kel.color("black")
        for i in range(7):
            kel.goto(self.posx, self.posy)
            kel.pendown()
            kel.forward(self.size*6)
            self.posy = self.posy+self.size;
            self.posx = initx;
            kel.penup()

        self.posx = initx;
        self.posy = inity;
        kel.left(90)
        for i in range(7):
            kel.goto(self.posx, self.posy)
            kel.pendown()
            kel.forward(self.size*6)
            self.posx = self.posx+self.size;
            self.posy = inity;
            kel.penup()

def draw_shape(self,initx, inity):
    self.posx = initx;
    self.posy = inity;
# Drawing yellow portion
    kel.penup()
    kel.goto(self.posx, self.posy)
    kel.pendown()
    kel.fillcolor("yellow")
    kel.begin_fill()
    kel.goto(self.posx, self.posy+self.size*6)
    kel.goto(self.posx+self.size*6, self.posy+self.size*6)
    kel.goto(self.posx+self.size*6, self.posy)
    kel.goto(self.posx, self.posy)
    kel.end_fill()

# Drawing red portion
    kel.penup()
    kel.goto(self.posx, self.posy+self.size*2)
    kel.pendown()
    kel.fillcolor("red")
    kel.begin_fill()
    kel.goto(self.posx+self.size, self.posy+self.size*3)
    kel.goto(self.posx, self.posy+self.size*4)
    kel.goto(self.posx+self.size*2, self.posy+self.size*4)
    kel.goto(self.posx+self.size*2, self.posy+self.size*6)
    kel.goto(self.posx+self.size*3, self.posy+self.size*5)
    kel.goto(self.posx+self.size*4, self.posy+self.size*6)
    kel.goto(self.posx+self.size*4, self.posy+self.size*4)
    kel.goto(self.posx+self.size*6, self.posy+self.size*4)
    kel.goto(self.posx+self.size*5, self.posy+self.size*3)
    kel.goto(self.posx+self.size*6, self.posy+self.size*2)
    kel.goto(self.posx+self.size*4, self.posy+self.size*2)
    kel.goto(self.posx+self.size*4, self.posy)
    kel.goto(self.posx+self.size*3, self.posy+self.size)
    kel.goto(self.posx+self.size*2, self.posy)
    kel.goto(self.posx+self.size*2, self.posy+self.size*2)
    kel.goto(self.posx, self.posy+self.size*2)
    kel.end_fill()

 # recursive function to draw repeating shape
def repeat_shape(self,intx, inty):
    if intx>=3 | inty>=3:
        return
    else:
        self.draw_shape(intx*180, inty*180)
        kel.right(90)
        self.repeat_shape(intx+1,inty)
        self.repeat_shape(intx,inty+1)

class image_play(object):
    def __init__(self,im_name):
        self.im_name = im_name

def rgb_to_gray_image(self):
    im = Image.open(self.im_name)
    im = im.convert('LA')

     # editing pixels of image to white
def loop_over_image(self):
    im = Image.open(self.im_name)
    width, height = im.size
    # nested loop over all pixels of image
    for i in range(width):
        for j in range(height):
            im[i][j] = 255;

kel.speed("fastest")

# object of class Draw_pattern
obj = Draw_pattern(0,0,30)
obj.draw_grid(0,0)
obj.repeat_shape(0,0)

AttributeErrorTraceback (most recent call last)
<ipython-input-14-8b7bbac27093> in <module>
    103 obj = Draw_pattern(0,0,30)
    104 obj.draw_grid(0,0)
--> 105 obj.repeat_shape(0,0)

AttributeError: 'Draw_pattern' object has no attribute 'repeat_shape'

奇怪的是,如果我使用一个不带循环的单片,它会很漂亮,并将xts“ ABT”转换为小标题“ ABT”

code <- c('ABT','BA','CL','ROK')

for (i in code)
  {
  i <- tk_tbl(i, preserve_index = TRUE, rename_index = "index",
       timetk_idx = FALSE, silent = FALSE)
}

第一个代码的错误消息是

  

警告:没有索引可保留。否则将对象转换为小标题   成功。

38:在tk_tbl.data.frame(as.data.frame(数据)中,preserve_index,...:

编辑:

ABT <- tk_tbl(ABT, preserve_index = TRUE, rename_index = "index", timetk_idx = FALSE, silent = FALSE) 是程序包tk_tabl中的函数,它“强制时序对象进行微调。”
timetk是一个包含名称的向量。

code

1 个答案:

答案 0 :(得分:0)

对我来说,您似乎希望<-assign正在做的事情。

我认为将循环更改为:

for (i in code) {
  assign(i, tk_tbl(i, preserve_index = TRUE, rename_index = "index", timetk_idx = FALSE, silent = FALSE))
}