在R中,假设我有以下数据集:
st<-data.frame(codigo=c("24","24","24","24","25","25","25","25"),
color=c("grey","blue","white","white","black","blue","green","black"),
talle=c(1:4,1:4),
precio=c("200","200","200","400","100","100","100","100"),
codigo2=c("24","NA","NA","24","25","NA","NA","NA"),
color2=c("NA","NA","NA","NA","NA","NA","NA","NA"),
talle2=c("NA","NA","NA","4","NA","NA","NA","NA"),
precio2=c("200","NA","NA","400","100","NA","NA","NA"))
有";codigo2"color2"talle2"lista2"precio2";使用左侧列的值创建的右侧列。lef中的行与列的值"0"不同;codigo"talle";和/或";价格";。我想做的是,当";codigo";的值与上级行相同;precio";下一行是作为上一行的一些,所有下一行必须是空的,然而,如果";precio";更改,则内部行必须显示其值,并且仅显示";codigo"talle";以及";precio"。如果";codigo";下一行与上一行相比是不同的,则下一行必须具有值"0";codigo";以及";precio";,然后再次重复该过程,直到行结束。
clearley我没有足够的知识来做这件事,我试过这个:
preciosdupli2<- df %>% group_by(Artículo,Talle,LISTA1) %>% filter (!duplicated(Artículo,Talle,LISTA1)
还有这个:
talleunico<-df%>%distinct(Talle) preciosdupli2<- df %>% group_by(Artículo,Talle,LISTA1) %>% filter (!duplicated(Artículo,Talle==talleunico,LISTA1))
从文本中计算出逻辑条件有点困难。下一行是前一行吗?上级行是当前行还是下一行?
以下是一些可能让你开始的代码:
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))
展开逻辑条件,你可能会到达你需要的地方。请注意,您确实应该提供一个可复制的示例,例如,请参阅此处。我不得不将你的数据复制到csv文件中,并将其读取到R.中