2017-08-30 18 views
0

我一直试图将数据框分解为以每列的标题命名的不同变量。RStudio中所有变量的输出相同

下面的代码运行所有列的分解,但是在所有创建的变量中输出相同的结果。我觉得有什么是错的“在colnames为(j(DF)”命令,但我不能想出什么。

for (i in 1:ncol(df)) 
{ 
    x <- (i) 
    for(j in colnames(df)){ 
    assign(j,(stl(df[,x], s.window="periodic"))) 
    } 
} 

东风的结构为时间序列表,每列都有一个名称等一系列的价格。

Serie1

2007年1月10.44

2007年2月5.75

2007年3月6.52

2007四月13.90

2007年5月12.25

君7.37

2007年7月3.51

回答

0

希望这有助于!

#sample data 
df <- data.frame(col1 = c(100:65), col2 = c(1:36)) 

stl_df <- list() 
for(i in 1:ncol(df)){ 
    #convert column's data into timeseries data 
    tsData = ts(df[,i],frequency = 12,start=2007) 
    #calculate 'stl' and store it in a list 
    stl_df[colnames(df)[i]] <- stl(tsData,s.window="periodic") 
} 
stl_df 
+0

它现在正在工作,谢谢! – user3408750

+0

很高兴它帮助! – Prem