[R] how to loop this?[Solved]

antonio rodriguez antonio.raju at gmail.com
Fri Nov 24 11:34:46 CET 2006


Petr Pikal escribió:
> Hi
>
> when working with lists, considerable option is to look on lapply, 
> sapply, mapply or other *apply functions. If I am not mistaken you 
> can do
>
> ttt<-lapply(lasker, function(x) 
> data.frame(table(substr(names(subset(x, x >=4)), 1, 7))))
>
> to get a list with desired output.
>
> HTH
> Petr
>   

Dear Petr,

It works great!!

Many Thanks,

Antonio

>
> On 23 Nov 2006 at 21:06, antonio rodriguez wrote:
>
> Date sent:      	Thu, 23 Nov 2006 21:06:18 +0100
> From:           	antonio rodriguez <antonio.raju at gmail.com>
> To:             	R-Help <r-help at stat.math.ethz.ch>
> Subject:        	[R] how to loop this?
>
>   
>> Hi,
>>
>> I have the next procedure:
>>
>> t1<-data.frame(table(substr(names(subset(lasker[[1]], lasker[[1]] >=
>> 4)), 1, 7)))
>>
>> t1[1:5,]
>>
>>          Var1 Freq
>> 1 1988-02    3
>> 2 1988-03    1
>> 3 1988-04    1
>> 4 1988-05    2
>> 5 1988-06    3
>>
>> How to make a new list?, dataframe? having 189 elements in the
>> 'lasker' list:
>>
>>  > str(lasker[[1]])
>>  'table' int [, 1:1274] 1 1 3 2 1 5 4 1 1 4 ...
>>  - attr(*, "dimnames")=List of 1
>>   ..$ : chr [1:1274] "1988-01-13" "1988-01-16" "1988-01-20"
>>   "1988-01-25" ...
>> .
>> .
>>  > str(lasker[[189]])
>>  'table' int [, 1:464] 1 1 4 1 4 4 6 2 3 3 ...
>>  - attr(*, "dimnames")=List of 1
>>   ..$ : chr [1:464] "1999-07-21" "1999-07-23" "1999-07-25"
>>   "1999-07-31" ...
>>
>>
>> I've tried:
>>
>> windows<-vector("list",189)
>>
>> for (i in 1:189){
>> windows[[i]]<-data.frame(table(substr(names(subset(lasker[[i]], 
>> lasker[[i]] >= 4)), 1, 7)))
>> }
>>
>> Erro en rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
>>         invalid number of copies in rep.int()
>>
>> I think this is due because each new element (like t1) has 2 columns.
>> But what kind of structure is needed to accomodate the new elements?
>>
>> Thanks
>>
>> Antonio
>>



More information about the R-help mailing list