r - Inserting rows before each group -


i have following list , want add new row before each group of id's preserving id , setting , b 1.00.

       id      datee            b     102984 2016-11-23      2.0    2.0    140349 2016-11-23      1.5    1.5    167109 2017-04-16      2.0    2.0    167109 2017-06-21      1.5    1.5 

the end result:

  id      datee                b        102984    na           1.0    1.0   102984 2016-11-23      2.0    2.0          140349    na           1.0    1.0         140349 2016-11-23      1.5    1.5   167109    na           1.0    1.0                167109 2017-04-16      2.0    2.0          167109 2017-06-21      1.5    1.5        

up until used following code adds empty row @ bottom of each group do.call(rbind, by(df,df$id,rbind,"")) couldn't introduce specific values in respective columns when substituted "" vector of values.

here 1 option tidyverse. distinct rows of dataset 'id', mutate variables 'a', 'b' 1, , 'datee' na, bind_rows row bind original dataset , arrange 'id'

library(tidyverse) df1 %>%   distinct(id, .keep_all= true) %>%   mutate_at(vars("a", "b"), funs((1))) %>%    mutate(datee = na) %>%   bind_rows(., df1) %>%   arrange(id) #     id      datee     b #1 102984       <na> 1.0 1.0 #2 102984 2016-11-23 2.0 2.0 #3 140349       <na> 1.0 1.0 #4 140349 2016-11-23 1.5 1.5 #5 167109       <na> 1.0 1.0 #6 167109 2017-04-16 2.0 2.0 #7 167109 2017-06-21 1.5 1.5 

(i'll assume date formatting has been fixed, e.g., df1$datee = as.date(df1$datee).)


or translated base r:

new1 = data.frame(id = unique(df1$id), datee = sys.date()[na_integer_], = 1, b = 1) tabs = list(new1, df1) res  = do.call(rbind, tabs) res <- res[order(res$id), ]  #       id      datee     b # 1 102984       <na> 1.0 1.0 # 4 102984 2016-11-23 2.0 2.0 # 2 140349       <na> 1.0 1.0 # 5 140349 2016-11-23 1.5 1.5 # 3 167109       <na> 1.0 1.0 # 6 167109 2017-04-16 2.0 2.0 # 7 167109 2017-06-21 1.5 1.5 

or data.table:

library(data.table) new1 = data.table(id = unique(df1$id), datee = sys.date()[na_integer_], = 1, b = 1) tabs = list(new1, df1) res  = rbindlist(tabs) setorder(res)  #       id      datee     b #1: 102984       <na> 1.0 1.0 #2: 102984 2016-11-23 2.0 2.0 #3: 140349       <na> 1.0 1.0 #4: 140349 2016-11-23 1.5 1.5 #5: 167109       <na> 1.0 1.0 #6: 167109 2017-04-16 2.0 2.0 #7: 167109 2017-06-21 1.5 1.5 

there other ways, too:

# or let datee , other cols filled na library(data.table) new1 = data.table(id = unique(df1$id), = 1, b = 1) tabs = list(df1, new1) res  = rbindlist(tabs, fill = true, idcol = "src") setorder(res, id, -src) res[, src := null ]  # or more compact option (assuming df1$a has no missing values) library(data.table) setdt(df1)[, .sd[c(.n+1, seq_len(.n))], id][is.na(a), c("a", "b") := 1][] 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -