I'm completely stumped and seem to be going around for 2 days in the wrong circles. Here's what I have in 2 dataframes:> dim(params.data); head(params.data, n=2)[1] 284 6 item a1 a2 b1 b2 c 1 1 0.6671587 0.9588642 -3.5 -3.437961 0.2 2 2 1.1296298 1.4899307 -3.5 -4.146118 0.2> dim(examinees.data); head(examinees.data, n=2)[1] 1600 3 examinee theta1 theta2 1 1 -3 -3.133437 2 2 -3 -3.293341 I also have a function, p(), that takes on values for a1, a2, b1, b2, c, theta1, and theta2, then outputs a value. I would like to produce a 3-column array that is 284*1600 = 454400 rows long for my simulator. Somthing like: examinee item p 1 1 1 p-value 2 1 2 p-value ... 454399 1600 283 p-value 454400 1600 284 p-value My brain seems to be stuck in a for-loop loop. I'm sure there is an elegant and efficient R way to do this, but I can't seem to find the right brain prompts in help pages, forum searches, or my trusty R Book. So, I appreciate any guidance you might be able to provide. Cheers, Kev- -- View this message in context: http://www.nabble.com/Combining-2-arrays-into-a-third-array-via-a-function-tp22274922p22274922.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2009-Mar-01 19:49 UTC
[R] Combining 2 arrays into a third array via a function
Better to not use c as a variable name. It's such a useful function name. This is what I would have tried: col3 <- expand.grid(item=params.data$item, examinee=examinees.data $examinee) col3$p_val <- p( c( params.data[ col3["item"], c("a1", "a2", "b1", "b2", "c") ], examinees.data[ col3["examinee"], c("theta1", "theta2")] ) ) If you had provided executable test data I would have tested it, but I am getting rather bored with creating such for posters who won't follow the posting guide. If you had just used dput() around those calls to head() it would have been trivial. As it is, I am not sure if this will properly run. Could need a different "driving" construction to get the values of col3 to "feed through" to the row indices. Or have any number of stoopid errors. -- David Winsemius On Mar 1, 2009, at 11:47 AM, Kevski wrote:> > I'm completely stumped and seem to be going around for 2 days in the > wrong > circles. Here's what I have in 2 dataframes: > >> dim(params.data); head(params.data, n=2) > [1] 284 6 > item a1 a2 b1 b2 c > 1 1 0.6671587 0.9588642 -3.5 -3.437961 0.2 > 2 2 1.1296298 1.4899307 -3.5 -4.146118 0.2 > >> dim(examinees.data); head(examinees.data, n=2) > [1] 1600 3 > examinee theta1 theta2 > 1 1 -3 -3.133437 > 2 2 -3 -3.293341 > > I also have a function, p(), that takes on values for a1, a2, b1, > b2, c, > theta1, and theta2, then outputs a value. > > I would like to produce a 3-column array that is 284*1600 = 454400 > rows long > for my simulator. Somthing like: > examinee item p > 1 1 1 p-value > 2 1 2 p-value > ... > 454399 1600 283 p-value > 454400 1600 284 p-value > > My brain seems to be stuck in a for-loop loop. I'm sure there is an > elegant > and efficient R way to do this, but I can't seem to find the right > brain > prompts in help pages, forum searches, or my trusty R Book. So, I > appreciate > any guidance you might be able to provide. > > Cheers, > Kev- > -- > View this message in context: http://www.nabble.com/Combining-2-arrays-into-a-third-array-via-a-function-tp22274922p22274922.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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.