I Need to have a blank observation after each row in r using dataframe? -
this question has answer here:
output getting in dataframe
col1 col2 col3 col4 abc 25 35 abc asd 25 45 def 15 36 erf 23 69 erf asd 25 36 erf dfg 85 78 convert this
1. col1 col2 col3 col4 2. 3. abc 25 35 4. abc asd 25 45 5. abc def 15 36 6. 7. def erf 23 69 8. 8. erf asd 25 36 9. erf dfg 85 78 10 erf fgh 78 89 i need blank observation @ 2 6 , 8. there option?
if assume col1 , col2 of class character, quick solution be
res <- do.call(rbind, lapply(split(df, df$col1), function(x) rbind("", x))) row.names(res) <- null res # col1 col2 col3 col4 # 1 # 2 abc 25 35 # 3 abc asd 25 45 # 4 # 5 def 15 36 # 6 # 7 erf 23 69 # 8 erf asd 25 36 # 9 erf dfg 85 78 or using data.table package (which doesn't assume character class necessarily)
library(data.table) setdt(df)[df[, c(na, .i), = col1]$v1] # col1 col2 col3 col4 # 1: na na na na # 2: abc 25 35 # 3: abc asd 25 45 # 4: na na na na # 5: def 15 36 # 6: na na na na # 7: erf 23 69 # 8: erf asd 25 36 # 9: erf dfg 85 78
Comments
Post a Comment