如何根据另一个变量的行上的值添加一定数量的行

时间:2018-02-22 21:30:58

标签: r rbind newrow

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

我需要根据变量" number"创建追加行数。让日期和时间与编号行相同,而变量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 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

对于数据框df,此循环将使用rbind.fill(来自plyr)执行此操作:

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

答案 2 :(得分:0)

使用expandRowsseparate功能可以实现另一种选择。展开行将允许使用组合值复制rows,以后可以将其分开并添加到原始df

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

答案 3 :(得分: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