[R] A programming question - is what I want to do possible in R?
David Winsemius
dwinsemius at comcast.net
Mon Sep 28 14:06:48 CEST 2009
On Sep 27, 2009, at 10:45 PM, ewaters wrote:
>
> I have a large data frame, 77 rows, with 10 columns. Each row
> represents a
> unique individual with 10 characteristics, some of which are
> categorical
> factors and some continuous numeric variables.
Most of us would consider that a small dataframe, unless of course we
entered the values by hand as you may have.
> Each of the ten variables is
> important (the 10 columns obviously correspond to the individuals of
> interest). Importantly, this data set represents a population (not
> sample)
> of people with a certain medical condition.
You have the world's enumeration of persons with condition X? Besides
that obvious objection, if you really thought you had an entire
population, there would be little point in doing statistics through
random sampling.
>
> What I want to do is to select 2000 random samples of between 2 and 24
> individuals, preserving all the information.
>
> I can easily write loops that will sample from 1:77 2 - 24 times,
> what I
> really want to know is there any way to easily link the output of
> loops like
> that to the data set so I don't have to trawl through and do it
> manually
> 2000 times?
If you have a vector, vec of any length that represents a sample from
1:77 and your dataframe is df1, then you can use that index vector to
extract a group thusly:
df1[vec, ]
Example:
> set.seed(97)
> df1 <- data.frame(casenum=1:77, ht=rnorm(77, 56, 7), wt=rnorm(77,
160, 30) )
> df1[c(17, 29, 36, 55, 72), ]
casenum ht wt
17 17 62.68708 110.0956
29 29 69.97378 124.9440
36 36 49.97847 101.8919
55 55 57.46707 169.7421
72 72 52.25796 118.2071
>
> Any advice on whether I should even attempt that in R, or try some
> sort of
> hash table in C or somewhere, would be appreciated.
--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list