Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip value in RTF reporting

Tags:

r

I have a data frame df for report in RTF format as:

df <-

ATRSLBL POPUL   CENTRE  BAGE    BAGEC1  SEX
Red     PPS     37201   75      3       1
Red     PPS     37201   71      2       2
Red     PPS     37201   73      2       1
Red     PPS     38201   66      2       2
Blue    PPS     37201   78      3       2
Blue    PPS     38201   71      2       2
Blue    PPS     38201   71      2       1
Blue    PPS     38201   64      1       2

I want to print it as:

ATRSLBL POPUL   CENTRE  BAGE    BAGEC1  SEX
Red     PPS     37201   75      3       1
        PPS             71      2       2
        PPS             73      2       1
        PPS     38201   66      2       2
Blue    PPS     37201   78      3       2
        PPS     38201   71      2       2
        PPS             71      2       1
        PPS             64      1       2

Can anyone help me please.

like image 608
Abu Noman Avatar asked Oct 14 '15 09:10

Abu Noman


1 Answers

We can do this with data.table. We convert the 'data.frame' to 'data.table' (setDT(df)), We get the logical index of duplicated 'ATRSLBL' and assign (:=) it to ''. We create the grouping variable (cumsum(ATRSLBL !='')), and get the row index of duplicated 'CENTRE', use that index to assign 'CENTRE' to '' after converting the 'CENTRE' column to 'character'

library(data.table)
setDT(df)[duplicated(ATRSLBL), ATRSLBL := '']
i1 <- df[, .I[duplicated(CENTRE)] , cumsum(ATRSLBL!='')]$V1
df[, CENTRE:= as.character(CENTRE)][i1, CENTRE:= '']
df
#    ATRSLBL POPUL CENTRE BAGE BAGEC1 SEX
#1:     Red   PPS  37201   75      3   1
#2:           PPS          71      2   2
#3:           PPS          73      2   1
#4:           PPS  38201   66      2   2
#5:    Blue   PPS  37201   78      3   2
#6:           PPS  38201   71      2   2
#7:           PPS          71      2   1
#8:           PPS          64      1   2

NOTE: Here I am assuming the 'ATRSLBL' column as character class.

like image 187
akrun Avatar answered Sep 26 '22 04:09

akrun