为每个组添加行

时间:2018-03-23 13:24:38

标签: r

我的数据框如下:

       Serial_number Amplification Voltage
(...)
    51    912009913    50.8258    335.965
    52    912009913    54.3855    337.966
    53    912009913    58.3014    339.959
    54    912009913    62.6758    341.956
    55    912009913    67.5537    343.965
    56    912009913    72.9996    345.965
    57    912009913    79.1489    347.964
    58    912009913    86.1111    349.965
    59    912009913    94.0453    351.957
    60    912009913    103.2320    353.957
    61    912009913    113.9780    355.964
    62    912009913    126.5830    357.964
    63    912009913    141.6830    359.963
    64    912009913    160.0800    361.962
    65    912009913    182.8440    363.955
    66    912009913    211.9870    365.957
    67    912009913    250.4290    367.965
(...)
    119    912009897    50.8925    329.968
    120    912009897    54.5503    331.969
    121    912009897    58.5886    333.968
    122    912009897    63.0918    335.968
(...)

我想为每个Serial_number添加一行,以便我可以使用该行获取更多信息。行号/索引并不重要,但如果我可以考虑,我不会声称。

我该怎么做?

3 个答案:

答案 0 :(得分:2)

你可以尝试

library(tidyverse)
d <- data.frame(Serial_number=gl(5, 2), Amplification=1:10, Voltage=20:11)

d %>% 
  split(.$Serial_number) %>% 
  map(~add_row(., Serial_number=unique(.$Serial_number), Amplification=NA, Voltage=NA)) %>% 
  bind_rows()
   Serial_number Amplification Voltage
1              1             1      20
2              1             2      19
3              1            NA      NA
4              2             3      18
5              2             4      17
6              2            NA      NA
7              3             5      16
8              3             6      15
9              3            NA      NA
10             4             7      14
11             4             8      13
12             4            NA      NA
13             5             9      12
14             5            10      11
15             5            NA      NA

使用base R你可以尝试

do.call("rbind", lapply(split(d, d$Serial_number), function(x) rbind(x, c(1, NA, NA))))

当然,您可以rbind在Shique

的评论中提到的新数据框
rbind(d,
      data.frame(Serial_number=unique(d$Serial_number), Amplification=NA, Voltage=NA))

然后使用d[order(d$Serial_number),]

订购数据框

答案 1 :(得分:1)

使用data.table

的解决方案
require(data.table)
my.dt <- setDT(read.table(text = '"Serial_number" "Amplification" "Voltage"
                      912009913    50.8258    335.965
                      912009913    54.3855    337.966
                      912009913    58.3014    339.959
                      912009913    62.6758    341.956
                      912009913    67.5537    343.965
                      912009913    72.9996    345.965
                      912009913    79.1489    347.964
                      912009913    86.1111    349.965
                      912009913    94.0453    351.957
                      912009913    103.2320    353.957
                      912009913    113.9780    355.964
                      912009913    126.5830    357.964
                      912009913    141.6830    359.963
                      912009913    160.0800    361.962
                      912009913    182.8440    363.955
                      912009913    211.9870    365.957
                      912009913    250.4290    367.965
                      912009897    50.8925    329.968
                      912009897    54.5503    331.969
                      912009897    58.5886    333.968
                      912009897    63.0918    335.968',
                      header = TRUE))

head(my.dt)

# for each serial number, create a new data.table - put it in a list
my.serial.number.list <- lapply(my.dt[, unique(Serial_number)],
                            function(x) data.table(Serial_number = x))
# from list of data.tables to one data.table
temp.dt <- rbindlist(my.serial.number.list)

# bind with original data table
new.dt <- rbind(my.dt, temp.dt, fill = TRUE)
new.dt

答案 2 :(得分:1)

这里也可以使用经常被忽略的birth_country_id - 使用@Jimbou的数据

do