[R] Random number datasets help
Marc Schwartz
marc_schwartz at me.com
Fri Jun 19 22:56:41 CEST 2009
On Jun 19, 2009, at 10:25 AM, Alexandre Lockhart wrote:
>
> My other question involved formatting my output. Normally, my text
> file has 8 columns, each column with 500 values before the next 8
> below are generated, and so on until 28 are reached. I have
> examined formatting issues with each function (cbind, sink, apply,
> trying matrix (only one set is output)) in my revised code, but am
> unable to achieve my wanted output file: 7 columns of 500 generated
> values, four different times. Thank you.
>
> a1<-
> c
> (178.07,178.28,178.08,177.74,177.04,178.17,177.58,57.71,59.6,60.92,59.48,59.32,61.59,59.94,28.9,29.82,30.73,25.68,27.93,28.98,29.76,123.48,127.27,127.8,127.2,127.13,126.71,125.5
> )
> a2<-
> c
> (1.69,1.3,1
> ,.
> 18,1.53,1.31,1.35,1.83,1.56,1.12
> ,.
> 74,1.48,1.67,1.53
> ,.95,.87,0.03,1.12,1.95,1.22,1.04,1.64,1.83,1,1,1.08,1.35,2.37)
> sink(file='/home/lockhartag/Wim/TRP/output.txt',append=TRUE)
> apply(cbind(a1, a2), 1, function(x) rnorm(500, x[1], x[2]))
> sink()
Hi Alexandre,
Try this:
set.seed(1)
Result <- apply(cbind(a1, a2), 1, function(x) rnorm(500, x[1], x[2]))
# Take the 28 columns, split them up in groups of 7 columns and
rbind() them together
Result <- rbind(Result[, 1:7], Result[, 8:14], Result[, 15:21],
Result[, 22:28])
> str(Result)
num [1:2000, 1:7] 179 177 180 180 180 ...
That gives you a matrix with 7 columns and 2000 rows. The 2000 rows
will be 4 sets of 500 each, one set of 500 per time period in order.
You can then use:
write.table(Result, file = "out.txt")
to write 'Result' to a text file. Using the defaults above, the text
file will contain both row names and column names. See ?write.table
for more information.
HTH,
Marc
More information about the R-help
mailing list