Mr_Pag
October 4th, 2009, 05:10 PM
Hi all,
I'm doing a course in Statistical Modelling. I'm trying to convert a bit of SAS code to make a dataset with x points, my notes are in SAS and I'm hoping to do my work in R. I've been using R for about a week, but I've got nada programming experience so the following question about conditions and loops is a little confusing.
The SAS code looks like:
data sim;
do i=1 to 10000;
do x=1 to 10;
err = rannor(3921);
Y = 1+3*x+err;
output;
end;
end;
The code above has a loop within a loop, can anyone show me how to perform this task in the equivalent order in R. So far I've got (and quite proud of):
dataset <- matrix(ncol=4,nrow=10000)
dataset[,1]<- 1:10000
dataset[,2]<- 1:10
for (i in 1:nrow(dataset)) {
dataset[i,3]<- rnorm(1)
dataset[i,4] <- 1+3*dataset[i,2]+dataset[i,3]
}
I'd like to know how to put the generators for columns 1 and 2 inside the loop. I've tried all sorts of things - I'm sure this will come in useful later.
Thanks for your time folks,
Pag
I'm doing a course in Statistical Modelling. I'm trying to convert a bit of SAS code to make a dataset with x points, my notes are in SAS and I'm hoping to do my work in R. I've been using R for about a week, but I've got nada programming experience so the following question about conditions and loops is a little confusing.
The SAS code looks like:
data sim;
do i=1 to 10000;
do x=1 to 10;
err = rannor(3921);
Y = 1+3*x+err;
output;
end;
end;
The code above has a loop within a loop, can anyone show me how to perform this task in the equivalent order in R. So far I've got (and quite proud of):
dataset <- matrix(ncol=4,nrow=10000)
dataset[,1]<- 1:10000
dataset[,2]<- 1:10
for (i in 1:nrow(dataset)) {
dataset[i,3]<- rnorm(1)
dataset[i,4] <- 1+3*dataset[i,2]+dataset[i,3]
}
I'd like to know how to put the generators for columns 1 and 2 inside the loop. I've tried all sorts of things - I'm sure this will come in useful later.
Thanks for your time folks,
Pag