# Re: [R] Newbie: Simple loops: complex troubles

From: projection83 <mkurowski_at_gmail.com>
Date: Thu 05 Apr 2007 - 01:35:58 GMT

This helped a lot. Thank you so much - im now able to get some basic stuff moving around in R!

Petr Klasterecky wrote:
>
> Not sure whether this is exactly and everything you want, but at least
> it may give you some ideas how to proceed. You do not need loops at all:
>
> Let's try a simplified example with 3 samples, each of length 10 (just
> for printing purposes):
>
> m <- c(1,2,3)
> v <- c(1,4,9)
> n <- 10
> means <- rep(m,each=n)
> vars <- rep(v,each=n)
> means
> [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3
> vars
> [1] 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4 4 4 9 9 9 9 9 9 9 9 9 9
>
> numbers <- matrix(rnorm(length(means), mean=means, sd=sqrt(vars)),
> nrow=n, byrow=F)
> numbers
> [,1] [,2] [,3]
> [1,] 0.9721407 0.4510903 -2.880967
> [2,] -0.4834124 -2.7958993 -1.368037
> [3,] 1.6871736 -0.6717009 -3.268698
> [4,] 0.9738312 3.1919293 3.982135
> [5,] 0.8032162 1.0397078 7.227974
> [6,] -0.1606657 2.6339503 5.873210
> [7,] 0.5786295 -0.3589869 4.194425
> [8,] 0.9909184 2.0622899 6.432129
> [9,] 3.1687842 1.9765014 3.788201
> [10,] 1.4814704 3.3024049 4.194628
>
> colnames(numbers) <- paste('Ux',1:length(m),sep='')
> numbers
> Ux1 Ux2 Ux3
> [1,] 0.9721407 0.4510903 -2.880967
> [2,] -0.4834124 -2.7958993 -1.368037
> [3,] 1.6871736 -0.6717009 -3.268698
> [4,] 0.9738312 3.1919293 3.982135
> [5,] 0.8032162 1.0397078 7.227974
> [6,] -0.1606657 2.6339503 5.873210
> [7,] 0.5786295 -0.3589869 4.194425
> [8,] 0.9909184 2.0622899 6.432129
> [9,] 3.1687842 1.9765014 3.788201
> [10,] 1.4814704 3.3024049 4.194628
>
> Now your random vectors are in columns of 'numbers' and you can work
> with them using indexing.
>
> Petr
>
> projection83 napsal(a):

```>> I am used to java (well, i dont remember it really well, but anyway)
>>
>> I have having a really difficult time making simple loops to work. I got
>> the
>> following to work:
>>
>>          ##
>> 	##Creates objects Ux1, Ux2, Ux2 etc. that  all contain n numbers in a
>> random distribution
>> 	##
>>  	m<-c(m1,m2,m3,m4,m5,m6,m7,m8,m9,m10)#these are defined as numbers
>> (means)
>> 	v<-c(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10)#these are defined as numbers
>> (variances)
>>         n<-50
>> 	for(k in 1:g)
>> 	{
>> 		assign( paste("Ux", k, sep=""), rnorm( n  ,
>> assign(paste("m",1,sep=""),m[k])   ,   assign(paste("m",1,sep=""),v[k])
>> )
>> )
>> 	}
>>
>>
>> The above seems like a lot of work for such a simple feat, no?
>>
>> Also, I CANNot get the following to work in a loop manor:
>>
>>         Ux1i<-as.integer(Ux1)
>> 	Ux2i<-as.integer(Ux2)
>> 	Ux3i<-as.integer(Ux3)
>>
>> or
>>
>> 	Sx1<-sort(Ux1i)
>> 	Sx2<-sort(Ux2i)
>> 	Sx3<-sort(Ux3i)
>>
>> Maybe I am just not using matrixes enough? but even that seems quite a
>> lot
>> more complex than calling x<-matrix() then grabbing values by
>> x[j][k]...(java style if i remember correctly). the matrix help in R
>> dosnt
>> make much sense to me. And also i am not sure why numeric() dosnt make
>> you
>> define length before you use it, yet matrix() does.  Is there some other
>> funciton that i should be using to make length not an issue?
>>
>>
>> All in all, I dont know if i am going about this loop stuff a reaaaaly
>> round
>> about way - Any help would make me much less loopy:Pthanks
>>
>>
>>
```

>
> --
> Petr Klasterecky
> Dept. of Probability and Statistics
> Charles University in Prague
> Czech Republic
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
```--
View this message in context: http://www.nabble.com/Newbie%3A-Simple-loops%3A-complex-troubles-tf3523751.html#a9848704
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help