[R] Help understanding loop behaviour
PIKAL Petr
petr@p|k@| @end|ng |rom prechez@@cz
Fri Apr 30 08:43:35 CEST 2021
Hi
Your code is hardly readable as you used HTML formating (not recommended) so
I used another (split) approach.
Third column seems to be simple
#make list
lll <- split(as.factor(COMPANY_NUMBER), COMPANY_NUMBER)
#calculate sequences
as.numeric(unlist(lapply(lll, function(x) 1:length(x))))
should give you third column
The second column seems to be calculated this way.
lapply(lll, function(x) c(rep(1, length(x)-1), max(length(x))))
I believe others could come with simpler solutions.
BTW why result for
10057418
Should be different?
Cheers
Petr
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of e-mail
> ma015k3113 via R-help
> Sent: Thursday, April 29, 2021 5:41 PM
> To: r-help using r-project.org
> Subject: [R] Help understanding loop behaviour
>
> I am trying to understand how loops in operate. I have a simple dataframe
xx
> which is as follows
>
> COMPANY_NUMBER NUMBER_OF_YEARS
>
> #0070837 3
> #0070837 3
> #0070837 3
> 1000403 4
> 1000403 4
> 1000403 4
> 1000403 4
> 10029943 3
> 10029943 3
> 10029943 3
> 10037980 4
> 10037980 4
> 10037980 4
> 10037980 4
> 10057418 3
> 10057418 3
>
> 10057418 3
> 1009550 4
> 1009550 4
> 1009550 4
> 1009550 4
> The code I have written is
>
> while (i <= nrow(xx1) )
>
> {
>
> for (j in 1:xx1$NUMBER_OF_YEARS[i])
> {
> xx1$I[i] <- i
> xx1$J[j] <- j
> xx1$NUMBER_OF_YEARS_j[j] <- xx1$NUMBER_OF_YEARS[j] } i=i +
> (xx1$NUMBER_OF_YEARS[i] ) } After running the code I want my dataframe
> to look like
>
> |COMPANY_NUMBER |NUMBER_OF_YEARS| | I| |J|
>
> |#0070837 |3| |1| |1|
> |#0070837 |3| |1| |2|
> |#0070837 |3| |3| |3|
> |1000403 |4| |1| |1|
> |1000403 |4| |1| |2|
> |1000403 |4| |1| |3|
> |1000403 |4| |4| |4|
> |10029943 |3| |1| |1|
> |10029943 |3| |1| |2|
> |10029943 |3| |3| |3|
> |10037980 |4| |1| |1|
> |10037980 |4| |1| |2|
> |10037980 |4| |1| |3|
> |10037980 |4| |4| |4|
> |10057418 |3| |1| |1|
> |10057418 |3| |1| |1|
> |10057418 |3| |1| |1|
> |1009550 |4| |1| |1|
> |1009550 |4| |1| |2|
> |1009550 |4| |1| |3|
> |1009550 |4| |4| |4|
>
>
> I get the correct value of I but in the wrong row but the vaule of J is
correct in
> the first iteration and then it goes to 1
>
> Any help will be greatly appreciated
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list