Repeat the value of a line in the rows immediately below a dataframe in R

Asked

Viewed 162 times

-1

I need to write a function so that the values highlighted in yellow are repeated in the cells below, including overwriting those cells that already have values. Otherwise, the values to the left of the C100 fields (in red) need to be replicated in the cells to the left of the C170 and C190 fields. The problem is that the number of times these values should be repeated is variable, that is, I have to repeat these values in the cells below until the next C100 appears when they should start receiving the information from that last C100 and so on. The data below for those who can give a help. Thanks!

COD_PART    COD_MOD SER NUM_DOC C1
3070        55      1   94      C100
        30      287,25  0       C170
        30      161,22  0       C170
        15      116,05  0       C170
        7       54,16   0       C170
        8       61,89   0       C170
        5       31,37   0       C170
        15      80,6    0       C170
        5       31,37   0       C170
        7       54,16   0       C170
        10      75,56   0       C170
        8       61,89   0       C170
        5       36,49   0       C170
        8       61,89   0       C170
        7       54,16   0       C170
0       1168,06     0   0       C190
3104        55      1   288     C100
        10      42,41   0       C170
        10      42,41   0       C170
0       84,82       0   0       C190
3104        55      1   295     C100
        10      34,16   0       C170
        15      63,62   0       C170
        15      63,62   0       C170
        10      41,61   0       C170
        5       15,51   0       C170
0       218,52      0   0       C190
2463        55      1   369     C100
        8       170,94  0       C170
0       170,94      0   0       C190
2463        55      1   380     C100
        10      213,68  0       C170
0       213,68      0   0       C190
2463        55      1   391     C100
        10      213,68  0       C170
        10      82,36   0       C170
        10      85  0       C170
0       85      0   0       C190
0       296,04      0   0       C190
2463        55      1   399     C100
        10      213,68  0       C170
        10      82,36   0       C170
0       296,04      0   0       C190
3075        55      1   544     C100
        1       118,83  0       C170
        1       94,71   0       C170
        6       47,7    0       C170
        12      95,4    0       C170
        6       52,38   0       C170
1,25        409,02      5,11    0       C190

inserir a descrição da imagem aqui

  • 1

    Please consider always putting a reproducible example and a clear and objective question of your problem, read this.

1 answer

2


The following R base code solves the problem.

f <- cumsum(!is.na(dados$COD_PART))
dados$COD_PART <- ave(dados$COD_PART, f, FUN = function(x){x[-1] <- x[1]; x})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.