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列。
我该如何使此过程更快?
看起来不错,但我怎么能扩大到933行和324列的情况? – MERose 2014-10-27 11:52:52
@MERose它应该适用于扩展行数的情况。但是,您需要修改它以完成更多列所需的操作。我发现你的帖子不清楚你想在这种情况下做什么。也许你澄清你想要什么,并扩大你的数据示例了一下? – 2014-10-27 11:56:43
完成。它基本上是更多观察(每天行)和更多天(具有价格和时事性质的专题信息的专栏)。 – MERose 2014-10-27 16:12:32