# [R] Counting a number of "elements" in an object

Greg Gilbert gregory.e.gilbert at gmail.com
Thu Mar 25 13:06:49 CET 2010

```I apologize if this has been answered. I have researched this to the best of
my ability, that's not to say the answer isn't in the archives just I am a
new user and I don't know the proper terms to search under.

I have an object:

f <- mpr100 ~ time + nhb + hispanic + other +
rural + hrural +
factor(age) + factor(gender) + factor(mstat) +
factor(svcpct2) + nvaclass +
a1cgrp8 + anemdef + cbd + chf + chrnlung + htn_c +
hypothy + obese + perivasc  + pulmcirc + tumor +
depress + psych +
nhb*rural + hispanic*rural + other*rural +
nhb*hrural + hispanic*hrural + other*hrural +
nhb*factor(age)  + hispanic*factor(age) +
other*factor(age)  +
rural*factor(age) +  hrural*factor(age) +
(1|id) + (1|visn2)

that I pass to a function I wrote for bootstrapping (there are data-based
reasons I can't use existent bootstrapping functions). I create an empty
matrix and put the resulting coefficients for each model into the matrix and
thus need a matrix of size "repetitions" by "number of covariates"+1
(intercept). I create the empty matrix using the following:

results <- matrix(NA, nrow=reps, ncol=vars)

I pass the function the number of repetitions so creating the number of rows
is not a problem. At present I pass the function the number of variables
also. For simple models this works fine, but I need to run more complex
models.

I have need to dynamically create my empty matrix with the number of
repetitions for rows and the number of coefficients for columns (for example
age_cat has 5 levels and thus 4 coefficients), so the complexity of the
problem grows.

I thought about taking a small sample and running it through lmer to get
dummy results and use that number to create the matrix but that doesn't work
because the data frame is large and I run into problems running subsets of
the data frame against lmer, so this wouldn't be a universal solution.

Any help is appreciated. I apologize if I have not operationalized the
question sufficiently.

--
View this message in context: http://n4.nabble.com/Counting-a-number-of-elements-in-an-object-tp1690513p1690513.html
Sent from the R help mailing list archive at Nabble.com.

```