[R] lapply with different size lists?

Rui Barradas ruipbarradas at sapo.pt
Tue Sep 11 22:11:25 CEST 2012


Hello,

When you don't know what's going on, break long instructions into 
simpler ones.
R is good at doing a lot in one line, to debug don't do that.

b = function (m, mat) {
     n=nrow (mat)
     p=ceiling (n/m)
     lapply (1:p, function (l,n,m) {
         inf = ((l-1)*m)+1
         if (l<p) sup=((l-1)*m)+m else sup=n
         lst <- lapply( inf:sup, function(i)
             c(i, Inf, mat[i, 1], mat[i, 2]) )
         data.frame( matrix(lst, nrow=length(lst)) )
         #data.frame( matrix(unlist(lst), nrow=length(lst)) )
         }, n=n, m=m
     )
}

It returns a list of data.frames with 1 column only.
If you want 4 column data.frames, uncomment the instruction above (and 
comment out the previuos one).

Hope this helps,

Rui Barradas
Em 11-09-2012 19:44, Rui Esteves escreveu:
> Hello,
>
> I have 2 functions (a and b)
>
> a = function(n) { matrix (runif(n*2,0.0,1), n) }
>>
>> b = function (m, matrix) {
>>          n=nrow (matrix)
>>          p=ceiling (n/m)
>>          lapply (1:p, function (l,n,m) {
>>                          inf = ((l-1)*m)+1
>>                          if (l<p) sup=((l-1)*m)+m
>>                                  else sup=n
>>                          data.frame (matrix (lapply (inf: sup, function(i)
>> c(i, Inf, matrix[i,1], matrix[i,2]) ), nrow=m ) )
>>                  }, n=n, m=m
>>          )
>> }
>>
>>> my.matrix = a(7)
>>> my.matrix
>>              [,1]      [,2]
>> [1,] 0.708060983 0.3242221
>> [2,] 0.356736311 0.1454096
>> [3,] 0.402880340 0.4763676
>> [4,] 0.795947223 0.4052168
>> [5,] 0.001620093 0.2618591
>> [6,] 0.192215589 0.6595275
>> [7,] 0.539199304 0.5402015
>>
>>> b (m=6,matrix=my_matrix)
>> [[1]]
>>    matrix.lapply.inf.sup..function.i..c.i..Inf..matrix.i..1...matrix.i..
>> 1                                  1.0000000, Inf, 0.7080610, 0.3242221
>> 2                                  2.0000000, Inf, 0.3567363, 0.1454096
>> 3                                  3.0000000, Inf, 0.4028803, 0.4763676
>> 4                                  4.0000000, Inf, 0.7959472, 0.4052168
>> 5                            5.000000000, Inf, 0.001620093, 0.261859077
>> 6                                  6.0000000, Inf, 0.1922156, 0.6595275
>>
>> [[2]]
>>    matrix.lapply.inf.sup..function.i..c.i..Inf..matrix.i..1...matrix.i..
>> 1                                  7.0000000, Inf, 0.5391993, 0.5402015
>> 2                                  7.0000000, Inf, 0.5391993, 0.5402015
>> 3                                  7.0000000, Inf, 0.5391993, 0.5402015
>> 4                                  7.0000000, Inf, 0.5391993, 0.5402015
>> 5                                  7.0000000, Inf, 0.5391993, 0.5402015
>> 6                                  7.0000000, Inf, 0.5391993, 0.5402015
>>
> It seems like the second list is filled with repeated rows (from 2 to 6)
> I would like the second list to stop in the last row of the my_matrix
> So, I would like to have the following result:
>> b (m=6,matrix=my_matrix)
> [[1]]
>    matrix.lapply.inf.sup..function.i..c.i..Inf..matrix.i..1...matrix.i..
> 1                                  1.0000000, Inf, 0.7080610, 0.3242221
> 2                                  2.0000000, Inf, 0.3567363, 0.1454096
> 3                                  3.0000000, Inf, 0.4028803, 0.4763676
> 4                                  4.0000000, Inf, 0.7959472, 0.4052168
> 5                            5.000000000, Inf, 0.001620093, 0.261859077
> 6                                  6.0000000, Inf, 0.1922156, 0.6595275
>
> [[2]]
>    matrix.lapply.inf.sup..function.i..c.i..Inf..matrix.i..1...matrix.i..
> 1                                  7.0000000, Inf, 0.5391993, 0.5402015
>
>
> Can`t I do this with an apply function? Is there any more efficient way
> that substituting the lapply by a for loop?
>
> THanks,
> Rui
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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