如何每隔几行向数据框添加更多行

时间:2018-08-06 05:47:54

标签: r dataframe

我的数据框如下所示:

ID       A          B          C
 1       0          O          1
 1       0          P          2
 1       0          Q          3
 2       0          R          1
 2       0          S          2
 2       0          T          3
 3       0          U          1
 3       0          V          2
 3       0          W          3

我需要为每个ID添加48个新行(我有3个以上ID)。 对于列A:48个新单元格中的值等于1 对于B列:48个新单元格中的值等于最后一行中的值(Q代表1,T代表2,W代表3) 对于C列:需要从最后一行的值开始将值增加0.5,持续48次。

1 个答案:

答案 0 :(得分:0)

如果数据框是如此简单,则可以使用以下方法。如果您有更多的ID号,最好编写一个函数(第一个解决方案下方有更多详细信息)

ID <- rep(1:3, each = 3)
C <- rep(1:3, 3)
B <-  LETTERS[15:23]
DF <- data.frame(ID, "A" = 0, B, C)

# create 3 separate dataframes
DF1 <- DF [ DF$ID == 1,]
DF2 <- DF [ DF$ID == 2,]
DF3 <- DF [ DF$ID == 3,]

# apply the same routine on each dataframe
DF1[4:51, 'A'] <- 1
DF1[4:51, 'ID'] <- 1
DF1[4:51, 'B'] <- DF1[3, 'B']
DF1[4:51, 'C'] <- DF1[3, 'C'] + seq(from = 0.5,  by = 0.5,
                                length.out = 48)

DF2[4:51, 'A'] <- 1
DF2[4:51, 'ID'] <- 2
DF2[4:51, 'B'] <- DF2[3, 'B']
DF2[4:51, 'C'] <- DF2[3, 'C'] + seq(from = 0.5,  by = 0.5,
                                length.out = 48)
DF3[4:51, 'A'] <- 1
DF3[4:51, 'ID'] <- 3
DF3[4:51, 'B'] <- DF3[3, 'B']
DF3[4:51, 'C'] <- DF3[3, 'C'] + seq(from = 0.5,  by = 0.5,
                                length.out = 48)

# combine the 3 dataframe into 1
DFfinal <- rbind(DF1, DF2, DF3)
# reset the row numbers
rownames(DFfinal) <- NULL

如果ID的数量多于3,则最好编写一个用于拆分和创建DF'n'的函数,然后为每个ID运行该函数以构建完整的数据帧。

extendIDdf <- function(df, i) {
  dfi <- df[ID == i,]
  dfi[4:51, 'A'] <- 1
  dfi[4:51, 'ID'] <- 1
  dfi[4:51, 'B'] <- dfi[3, 'B']
  dfi[4:51, 'C'] <- dfi[3, 'C'] + seq(from = 0.5,  by = 0.5,
                                      length.out = 48)
  return (dfi)
}

uniqueIDs <- unique(DF$ID)
DFF <- DF[ID == "",]  # create an empty dataframe with correct columns

for ( i in uniqueIDs) {
  DFF <- rbind(DFF,   extendIDdf(DF, i)) # create the extended dataframe for the ID == i and then amends the running dataframe with it.
}
rownames(DFF) <-  NULL