[R-br] Criar uma coluna de lancamentos de pesca

Éder Comunello comunello.eder em gmail.com
Quarta Maio 11 18:47:39 BRT 2016


Humberto,

Você poderia ordenar o banco antes de rodar: df <- df[with(df, order(boat,
ano, mes, dia)),]

Ou modificar o código. Uma possibilidade:


### <code r>
df <- read.table(text=
"dia mes ano boat
13 1 2005 AGIOS.NIKOLAUS
10 1 2005 AGIOS.NIKOLAUS
11 1 2005 AGIOS.NIKOLAUS
12 1 2005 AGIOS.NIKOLAUS
9 1 2005 AGIOS.NIKOLAUS
10 1 2005 X
9 1 2005 X
12 1 2005 X
13 1 2005 Y
11 1 2005 Y", head=T, as.is=T)

df$lance <- NA
df

for (boat in unique(df$boat)) {
     sel <- which(df$boat==boat)
     ord <- with(df[sel,], order(dia, mes, ano))
     df[sel[ord], "lance"] <- 1:length(sel)
}

df
#    dia mes  ano           boat lance
# 1   13   1 2005 AGIOS.NIKOLAUS     5
# 2   10   1 2005 AGIOS.NIKOLAUS     2
# 3   11   1 2005 AGIOS.NIKOLAUS     3
# 4   12   1 2005 AGIOS.NIKOLAUS     4
# 5    9   1 2005 AGIOS.NIKOLAUS     1
# 6   10   1 2005              X     2
# 7    9   1 2005              X     1
# 8   12   1 2005              X     3
# 9   13   1 2005              Y     2
# 10  11   1 2005              Y     1
### </code>




​
================================================
Éder Comunello
Agronomist (UEM), MSc in Environ. Sciences (UEM)
DSc in Agricultural Systems Engineering (USP/Esalq)
Brazilian Agricultural Research Corporation (Embrapa)
Dourados, MS, Brazil |<O>|
================================================
GEO, -22.2752, -54.8182, 408m
UTC-04:00 / DST: UTC-03:00




Em 11 de maio de 2016 14:39, Humberto Hazin <hhazin em gmail.com> escreveu:

> Muito obrigado Rodrigo e Elder,
>
> parcialmente o problema foi resolvido, entretanto olhando o resultado
> abaixo do código do Elder no meu banco de dados teria que estar
> classificado dia mês ano barco para que a sequência de lances darem certos
> correto? Ou pode ser incluída no código?  Olhando o exemplo que esta em
> amarelo por exemplo o lance 3 deveria ser o lance 2.
>
>
>
>     set.day setyear setmonth              boat lance
>
> 1         2    1978        8 SUMIYOSHI.MARU.32     1
>
> 2         4    1978        8      SEIKO.MARU.8     1
>
> 3        16    1978        4 SUMIYOSHI.MARU.32     2
>
> 4        13    1978        4 SUMIYOSHI.MARU.32     3
>
> 5        23    1978        9      SEIKO.MARU.8     2
>
> 6         7    1978       10 SUMIYOSHI.MARU.32     4
>
> 7        27    1978        1 SUMIYOSHI.MARU.32     5
>
> 8        10    1978        1  SUMIYOSHI.MARU.8     1
>
>
>
>
>
>
>
>
>
> *De:* R-br [mailto:r-br-bounces em listas.c3sl.ufpr.br] *Em nome de *Éder
> Comunello
> *Enviada em:* quarta-feira, 11 de maio de 2016 11:23
> *Para:* r-br em listas.c3sl.ufpr.br
> *Assunto:* Re: [R-br] Criar uma coluna de lancamentos de pesca
>
>
>
> Humberto, bom dia!
>
>
>
> Segue uma uma sugestão, sem considerar que possa haver duplicidade...
>
>
>
> ### <code r>
>
> df <- read.table(text=
>
> "dia mes  ano  boat
>
> 9    1    2005 AGIOS.NIKOLAUS
>
> 10   1    2005 AGIOS.NIKOLAUS
>
> 11   1    2005 AGIOS.NIKOLAUS
>
> 12   1    2005 AGIOS.NIKOLAUS
>
> 13   1    2005 AGIOS.NIKOLAUS
>
> 9    1    2005 X
>
> 10   1    2005 X
>
> 11   1    2005 X
>
> 12   1    2005 Y
>
> 13   1    2005 Y", head=T, as.is=T)
>
>
>
> df$lance <- NA
>
> df
>
>
>
> for (boat in unique(df$boat)) {
>
>      sel <- which(df$boat==boat)
>
>      df[sel, "lance"] <- 1:length(sel)
>
> }
>
>
>
> df
>
> #    dia mes  ano           boat lance
>
> # 1    9   1 2005 AGIOS.NIKOLAUS     1
>
> # 2   10   1 2005 AGIOS.NIKOLAUS     2
>
> # 3   11   1 2005 AGIOS.NIKOLAUS     3
>
> # 4   12   1 2005 AGIOS.NIKOLAUS     4
>
> # 5   13   1 2005 AGIOS.NIKOLAUS     5
>
> # 6    9   1 2005              X     1
>
> # 7   10   1 2005              X     2
>
> # 8   11   1 2005              X     3
>
> # 9   12   1 2005              Y     1
>
> # 10  13   1 2005              Y     2
>
> ### </code>
>
>
>>
> ================================================
> Éder Comunello
>
> Agronomist (UEM), MSc in Environ. Sciences (UEM)
>
> DSc in Agricultural Systems Engineering (USP/Esalq)
>
> Brazilian Agricultural Research Corporation (Embrapa)
>
> Dourados, MS, Brazil |<O>|
>
> ================================================
>
> GEO, -22.2752, -54.8182, 408m
>
> UTC-04:00 / DST: UTC-03:00
>
>
>
>
>
>
>
>
>
> Em 11 de maio de 2016 09:37, Humberto Hazin <hhazin em gmail.com> escreveu:
>
> Olá pessoal,
>
>
>
> Estou precisando criar uma coluna chamada lances. Onde cada lance
> corresponde a um dia, mês, ano e barco conforme o exemplo abaixo:
>
>
>
>            dia
>
> mes
>
> ano
>
> boat
>
> 9
>
> 1
>
> 2005
>
> AGIOS.NIKOLAUS
>
> 10
>
> 1
>
> 2005
>
> AGIOS.NIKOLAUS
>
> 11
>
> 1
>
> 2005
>
> AGIOS.NIKOLAUS
>
> 12
>
> 1
>
> 2005
>
> AGIOS.NIKOLAUS
>
> 13
>
> 1
>
> 2005
>
> AGIOS.NIKOLAUS
>
>
>
>
>
> Eu gostaria que a tabela ficasse dessa forma aqui
>
>
>
> dia
>
> mes
>
> ano
>
> boat
>
> Lance
>
> 9
>
> 1
>
> 2005
>
> AGIOS.NIKOLAUS
>
> 1
>
> 10
>
> 1
>
> 2005
>
> AGIOS.NIKOLAUS
>
> 2
>
> 11
>
> 1
>
> 2005
>
> AGIOS.NIKOLAUS
>
> 3
>
> 12
>
> 1
>
> 2005
>
> AGIOS.NIKOLAUS
>
> 4
>
> 13
>
> 1
>
> 2005
>
> AGIOS.NIKOLAUS
>
> 5
>
>
>
>
>
> Agradeço desde já
>
>
>
> Humberto
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> R-br mailing list
> R-br em listas.c3sl.ufpr.br
> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
> Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e forneça
> código mínimo reproduzível.
>
>
>
-------------- Próxima Parte ----------
Um anexo em HTML foi limpo...
URL: <http://listas.inf.ufpr.br/pipermail/r-br/attachments/20160511/dfcd8ebc/attachment.html>


Mais detalhes sobre a lista de discussão R-br