简体   繁体   中英

I need to compare the superior row with the one below, so I can create an empty one or with the infeiror's content, depending on that comparison

In R, I need to creat the colums "codigo", "color", "talle", "lista","precio" in the right using the values of the some ones of the left. What I want to do is that when the value of "codigo" of the inferior row is the same as the superior row and the value of "precio" of the inferior row is the some as the superior row, all the inferior row must be empty, however, if "precio" changes, then the infirior row must show its value of, and only, "codigo", "talle" and "precio". If the value of "codigo" of the inferior row compared with the superior row is different, then the inferior row must have the value of "codigo" and "precio", and then repete the procedure all over again till the end of the rows.

clearley I don´t have enough knowledge to do this, I´ve tried this:

    preciosdupli2<- df %>% group_by(Artículo,Talle,LISTA1) %>% filter (!duplicated(Artículo,Talle,LISTA1)

and also this:

    talleunico<-df%>%distinct(Talle) preciosdupli2<- df %>% group_by(Artículo,Talle,LISTA1) %>% filter (!duplicated(Artículo,Talle==talleunico,LISTA1))
codigo color talle lista precio codigo color talle lista precio
26 GRIS 1 LISTA1 9890 26 9890
26 NEGR 1 LISTA1 9890
26 2 LISTA1 9890
26 ACQU 2 LISTA1 9890
26 AZUL 2 LISTA1 9890
26 GRIS 2 LISTA1 9890
26 NEGR 3 LISTA1 9890
26 ROJO 4 LISTA1 12202 26 4 12202
26 SALM 5 LISTA1 12203 26 5 12203
28 GRIS 1 LISTA1 2150 28 2150
29 AZUL 1 LISTA1 2390 29 2390
29 ROJO 2 LISTA1 2390
29 VERD 3 LISTA1 2390
29 VIOL 4 LISTA1 2500 29 4 2500
29 AZUL 5 LISTA1 3300 29 5 3300
29 ROJO 6 LISTA1 3300 29 6 3300
29 VERD 6 LISTA1 3300 29 6 3300
29 VIOL 7 LISTA1 3333 29 7 3333

It's a little hard to work out the logical conditions from the text. Is the inferior row the previous row? Is the superior row the current or next row?

Here's a bit of code that might get you started:

library(dplyr)

data <- read.csv("./73739092/data.csv")

data2 <- data %>%
  mutate(codigo_2 = if_else(lag(precio, 1) != precio |
                              is.na(lag(precio, 1)), codigo, 0L))

Expand on the logical conditions and you may get where you need to be. Note that you should really offer a reproducible example, for instance see here . I had to copy your data into a csv file and read it into R.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM