[R] Loop for in R to generate several variables

Gustaf Rydevik gustaf.rydevik at gmail.com
Mon Apr 7 12:48:03 CEST 2008


On Mon, Apr 7, 2008 at 11:31 AM, arpino <bruno.arpino at unibocconi.it> wrote:
>
>  Hi everybody,
>  I have to create several variables of this form:
>
>  Yind = L0 + L1*X1 + L2*X2 + L3*X3 + K*Cind + n
>
>  where ind varires in {1,...,10}
>
>  I thought to this loop for but it does not work:
>
>  for (ind in 1:10) {
>
>   Yind = L0 + L1*X1 + L2*X2 + L3*X3 + K*Cind + n
>
>
>         }
>
>  Any suggestions?
>
>  Thank you.
>
>

look up ?assign and ?get, i.e:
for (ind in 1:10) {
assign(paste("Y",ind,sep=""),L0 + L1*X1 + L2*X2 + L3*X3 +
get(paste("C",ind,sep=""))+ n)
 }

regards,

Gustaf

-- 
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik



More information about the R-help mailing list