拥有大量数据,并希望能够在日期为01-31的行之后添加一行 一个例子:
id place name date Measure
1 24 aa 1989-01-29 0.6
1 24 aa 1989-01-30 0.47
1 24 aa 1989-01-31 0.25
1 24 bb 1989-01-29 0.18
1 24 bb 1989-01-30 0.37
1 24 bb 1989-01-31 0.81
1 25 aa 1989-01-29 0.92
1 25 aa 1989-01-30 0.43
1 25 aa 1989-01-31 0.8
2 24 aa 1989-01-29 0.75
2 24 aa 1989-01-30 0.15
2 24 aa 1989-01-31 0.61
我正在寻找这样的输出,运行时间很重要(我有大量数据)
id place name date Measure
1 24 aa 1989-01-29 0.6
1 24 aa 1989-01-30 0.47
1 24 aa 1989-01-31 0.25
0 0 0 0 0
1 24 bb 1989-01-29 0.18
1 24 bb 1989-01-30 0.37
1 24 bb 1989-01-31 0.81
0 0 0 0 0
1 25 aa 1989-01-29 0.92
1 25 aa 1989-01-30 0.43
1 25 aa 1989-01-31 0.8
0 0 0 0 0
2 24 aa 1989-01-29 0.75
2 24 aa 1989-01-30 0.15
2 24 aa 1989-01-31 0.61
任何想请
答案 0 :(得分:0)
由于您的数据框具有一些非数字列(“日期”和“名称”),因此使用NA
而不是0
创建新行可能更有意义。这是一种实现方法:
# the data as provided
df <- read.table(text = 'id place name date Measure
1 24 aa 1989-01-29 0.6
1 24 aa 1989-01-30 0.47
1 24 aa 1989-01-31 0.25
1 24 bb 1989-01-29 0.18
1 24 bb 1989-01-30 0.37
1 24 bb 1989-01-31 0.81
1 25 aa 1989-01-29 0.92
1 25 aa 1989-01-30 0.43
1 25 aa 1989-01-31 0.8
2 24 aa 1989-01-29 0.75
2 24 aa 1989-01-30 0.15
2 24 aa 1989-01-31 0.61', header = T)
# indices of matching dates, then turned into a rolling sum
key.dates <- cumsum(grepl('01-31$', df$date))
# a blank data frame to store padded data
df.new <- data.frame(id = rep(NA, nrow(df) + max(key.dates)), place = NA, name = as.character(NA), date = as.Date(NA), Measure = NA, stringsAsFactors = F)
# copy the old data frame into the new, using the padded indices
df.new[1:nrow(df) + key.dates, ] <- df
id place name date Measure
1 1 24 1 1989-01-29 0.60
2 1 24 1 1989-01-30 0.47
3 NA NA <NA> <NA> NA
4 1 24 1 1989-01-31 0.25
5 1 24 2 1989-01-29 0.18
6 1 24 2 1989-01-30 0.37
7 NA NA <NA> <NA> NA
8 1 24 2 1989-01-31 0.81
9 1 25 1 1989-01-29 0.92
10 1 25 1 1989-01-30 0.43
11 NA NA <NA> <NA> NA
12 1 25 1 1989-01-31 0.80
13 2 24 1 1989-01-29 0.75
14 2 24 1 1989-01-30 0.15
15 NA NA <NA> <NA> NA
16 2 24 1 1989-01-31 0.61