0
date        time    td  number
20150102    80000   -1  0
20150102    80001   -1  2
20150102    80002   1   0
20150102    80003   1   3
20150102    80004   -1  0

我需要根据变量“数字”创建追加行数。并让日期和时间与编号的行相同,而变量 td=0。我想要这样的数据:

date        time    td  number
20150102    80000   -1  0
20150102    80001   -1  2
20150102    80002   1   0
20150102    80003   1   3
20150102    80004   -1  0
20150102    80001   0   NA 
20150102    80001   0   NA
20150102    80003   0   NA
20150102    80003   0   NA
20150102    80003   0   NA
4

4 回答 4

2

我会生成每一列,然后将它们绑定到一个数据框中,然后将它们绑定到原始数​​据框!无需循环。

假设您的数据框称为 df

#Create the date and time using the number column directly.
date <- rep(df$date, times = df$number)
time <- rep(df$time, times = df$number)

#Combine these fields into a data frame and set td to all 0s and number to all NAs
appenddf <- data.frame(date = date, time = time, td = 0, number = NA)

#Bind the data for appending to the original data frame
df <- rbind(df, appenddf)
于 2018-02-22T21:48:53.247 回答
0
> a=rep(1:nrow(dat),dat$number+1)
> transform(dat[c(a[!duplicated(a)],a[duplicated(a)]),-4],num=`length<-`(dat$number,length(a)))
        date  time td num
1   20150102 80000 -1   0
2   20150102 80001 -1   2
3   20150102 80002  1   0
4   20150102 80003  1   3
5   20150102 80004 -1   0
2.1 20150102 80001 -1  NA
2.2 20150102 80001 -1  NA
4.1 20150102 80003  1  NA
4.2 20150102 80003  1  NA
4.3 20150102 80003  1  NA
于 2018-02-23T08:47:21.697 回答
0

此循环将使用 rbind.fill (来自 plyr),用于数据帧df

for (i in length(df$n)){
  x = df$n[i]
  while (x > 0){
    df <- rbind.fill(df, df[i,1:2])
    x = x -1
    print(x)
  }
}
#Switch NA's in df$td column to 0
df$td[is.na(df$td)] <- 0
于 2018-02-22T21:47:34.857 回答
0

expandRows使用andseparate函数可以实现另一种选择。扩展行将允许rows使用组合值进行复制,这些值稍后可以分离出来并添加到 originaldf中。

library(splitstackshape)
library(dplyr)
df1 <- setDT(expandRows(df, "number"))[, newsamp := 
sprintf("%d-%d-%d-%d", date, time, 0, NA)][,newsamp] %>% as.data.frame() %>% 
  separate(1,c("date", "time", "td", "number"))

rbind(df, df1)

#Result
#       date  time td number
#1  20150102 80000 -1      0
#2  20150102 80001 -1      2
#3  20150102 80002  1      0
#4  20150102 80003  1      3
#5  20150102 80004 -1      0
#6  20150102 80001  0     NA
#7  20150102 80001  0     NA
#8  20150102 80003  0     NA
#9  20150102 80003  0     NA
#10 20150102 80003  0     NA
于 2018-02-22T22:07:51.880 回答