[R] Simplifying my code

jim holtman jholtman at gmail.com
Sun Nov 27 19:04:39 CET 2011


Consider the use of a 'list'

dat <- lapply(1:20, function(i) complete(dat.mice, i))


On Sun, Nov 27, 2011 at 12:52 PM, R. Michael Weylandt
<michael.weylandt at gmail.com> <michael.weylandt at gmail.com> wrote:
> But it's much better to preallocate memory:
>
> dat = vector("list", 20)
> or
> dat = vector("numeric", 20)
> or
> dat = matrix(nrow = 20, ncol = ??)
>
> as needed (put the right # of columns in for the matrix)
>
> Michael
>
> On Nov 27, 2011, at 11:24 AM, andrija djurovic <djandrija at gmail.com> wrote:
>
>> Hi.
>> You haven't specified object to store results.
>>
>> Try something like:
>>
>> dat = c() #or dat = list() or matrix with specified ncol and nrow
>> for(i in 1:20){
>>
>> dat[i]=complete(dat.mice,[i]
>>
>> }
>>
>> On Sun, Nov 27, 2011 at 4:02 PM, Christopher Desjardins <desja004 at umn.edu>wrote:
>>
>>> Hi,
>>> I have a pretty simple problem. Here is the code:
>>>
>>> dat1=complete(dat.mice,1)
>>>
>>> dat2=complete(dat.mice,2)
>>>
>>> dat3=complete(dat.mice,3)
>>>
>>> dat4=complete(dat.mice,4)
>>>
>>> dat5=complete(dat.mice,5)
>>>
>>> dat6=complete(dat.mice,6)
>>>
>>> dat7=complete(dat.mice,7)
>>>
>>> dat8=complete(dat.mice,8)
>>>
>>> dat9=complete(dat.mice,9)
>>>
>>> dat10=complete(dat.mice,10)
>>>
>>> dat11=complete(dat.mice,11)
>>>
>>> dat12=complete(dat.mice,12)
>>>
>>> dat13=complete(dat.mice,13)
>>>
>>> dat14=complete(dat.mice,14)
>>>
>>> dat15=complete(dat.mice,15)
>>>
>>> dat16=complete(dat.mice,16)
>>>
>>> dat17=complete(dat.mice,17)
>>>
>>> dat18=complete(dat.mice,18)
>>>
>>> dat19=complete(dat.mice,19)
>>>
>>> dat20=complete(dat.mice,20)
>>>
>>>
>>> I would like to simplify this into a for loop. I thought this would work:
>>>
>>> for(i in 1:20){
>>>
>>> dat[i]=complete(dat.mice,[i]
>>>
>>> }
>>>
>>> But it doesn't. Any tips?
>>>
>>> Chris
>>>
>>>       [[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.
>>>
>>
>>    [[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.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list