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

From: jim holtman <jholtman_at_gmail.com>
Date: Wed 04 Apr 2007 - 11:51:31 GMT

One of the things about R that you have to learn is vector operations. You try to avoid loops and also 'generating' variables -- this is where a list comes in very handy. To relate to Java, it is similar to 'struct'. Here is a program that does what you want to do; it uses lists and vectorized operations.

# generate some test data

m <- seq(5, 30, length=10)  # means
v <- m / 4  # variance
n <- 50   # samples

# use list for the data
ans <- lapply(seq_along(m), function(x) rnorm(n, m[x], v[x])) # convert to integer
ansInt <- lapply(ans, as.integer)
# sort
ansSort <- lapply(ansInt, sort)
# show the results
ansSort

On 4/3/07, projection83 <mkurowski@gmail.com> wrote:
>
>
> 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
>
>
>
> --
> View this message in context:
> http://www.nabble.com/Newbie%3A-Simple-loops%3A-complex-troubles-tf3523751.html#a9830574
> 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
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]

______________________________________________
R-help@stat.math.ethz.ch 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.
Received on Wed Apr 04 21:55:07 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 04 Apr 2007 - 12:31:01 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.