1
x = data.frame(Índice = c(1,2,3,4,5,6,7,8,9,10),
Data = as.Date(c("2019-01-22", "2019-01-21", "2019-01-20", "2019-01-18", "2019-01-17", "2019-01-16", "2019-01-15", "2019-01-14", "2019-01-11", "2019-01-10")),
Valor = c(20,25,28,42,59,18,72,63,18,42),
Busca = c(59,18,0,0,42,28,0,18,0,50))
I have a data frame x
, and in the column of the name Busca
, where the value is greater than "0", I need to search this number in the column Valor
and locate the first value equal to the one searched by following the indexes and insert it into a new column in the same index that is in the column Valor
.
In this way, for example, the value "18" would be returned in the index "6" in a new column. The value "42" in the index "10" and so on.
As an example I leave the same data frame below with the column Encontrado
how the search should return.
x = data.frame(Índice = c(1,2,3,4,5,6,7,8,9,10),
Data = as.Date(c("2019-01-22", "2019-01-21", "2019-01-20", "2019-01-18", "2019-01-17", "2019-01-16", "2019-01-15", "2019-01-14", "2019-01-11", "2019-01-10")),
Valor = c(20,25,28,42,59,18,72,63,18,42),
Busca = c(59,18,0,0,42,28,0,18,0,50),
Encontrado = c(0,0,0,0,59,18,0,0,18,42))
That would solve part of the problem. But note that in the value "Found" I left Found = c(0,0,0,0,59,18,0,0,18,42), the value "28" does not appear equal in your table, because this value is at a date prior to which it was searched, so it would not have to appear. The same thing would happen with "42", which would have to appear only in the last position, because the date from where it is being sought is older. Only the search has to be done from the date found in the "Search" column, following on older dates.
– Thallys Geovany