2014-10-27 131 views
0

我有一个面板,看起来像比较嵌套循环

df <-read.table(text=" 
preis10_6 akt_datum10_6 preis11_6 akt_datum11_6 
1.55  10.06.2014 1.45  10.06.2014 
1.56  09.06.2014 1.49  11.06.2014 
",header=TRUE,sep="") 

在奇数列(preise)包含收集在那一天,而偶数列(akt_datum)包含信息的价格与列标题进入细胞关于数据的话题性。因此,当df[2, 2]09.06.2014这意味着df[1:2, 2]中的信息是从前一天开始的。我想设置这些案例NA

这是我迄今所做的:

# Instrall stringr for the function str_sub 
require(stringr) || install.packages("stringr") 

# Get indices for the columns with topicality information 
spalten <- seq(2, length(df), 2) 

# Loop over these columns 
for (spalte in spalten) { 

    # Construct the benchmark date from the column name 
    splitter <- str_sub(names(df)[spalte], 10, -1) 
    splitter <- strsplit(splitter, "_") 
    # Account for the case where the column name is in short time format (no trailing 0) 
    splitter[[1]][1] <- ifelse(nchar(splitter[[1]][1])==1, 
          paste0("0", splitter[[1]][1]), 
          splitter[[1]][1] 
) 
    splitter[[1]][2] <- ifelse(nchar(splitter[[1]][2])==1, 
          paste0("0", splitter[[1]][2]), 
          splitter[[1]][2] 
) 
    date <- paste(splitter[[1]][1], splitter[[1]][2], "2014", sep=".") 

    # Loop over all rows in the actual column 
    for (zeile in 1:nrow(df)) { 
    # and set the cell and the one before equal to NA 
    ifelse(df[zeile, spalte]!=date, df[zeile, spalte] <- df[zeile, spalte-1] <- NA, "") 
    } 

} 

这工作,但需要年龄,因为我有事实933x324面板。也就是说,我每天有933个价格和时事信息,总共162天,这使得162个价格热门对= 324列。

我该如何使此过程更快?

回答

2

这是做你想做的吗?至少它可以产生与你的代码相同的输出(以换算的日期为模)。

# Read data example 
df <-read.table(text=" 
preis10_6 akt_datum10_6 preis11_6 akt_datum11_6 
1.55  10.06.2014 1.45  10.06.2014 
1.56  09.06.2014 1.49  11.06.2014 
",header=TRUE,sep="") 

# Convert to date (to allow for comparison between dates) 
df$akt_datum10_6 <- as.Date(df$akt_datum10_6, "%d.%m.%Y") 
df$akt_datum11_6 <- as.Date(df$akt_datum11_6, "%d.%m.%Y") 

# Check which date is first, and substitute `NA`s 
first <- df$akt_datum10_6 < df$akt_datum11_6 
df[first, 1:2] <- NA 
df[!first, 3:4] <- NA 
print(df) 
#preis10_6 akt_datum10_6 preis11_6 akt_datum11_6 
#1  1.55 2014-06-10  NA   <NA> 
#2  NA   <NA>  1.49 2014-06-11 
+0

看起来不错,但我怎么能扩大到933行和324列的情况? – MERose 2014-10-27 11:52:52

+0

@MERose它应该适用于扩展行数的情况。但是,您需要修改它以完成更多列所需的操作。我发现你的帖子不清楚你想在这种情况下做什么。也许你澄清你想要什么,并扩大你的数据示例了一下? – 2014-10-27 11:56:43

+0

完成。它基本上是更多观察(每天行)和更多天(具有价格和时事性质的专题信息的专栏)。 – MERose 2014-10-27 16:12:32