I would like to create a random sample of the rows of a data frame that
is larger than the number of rows in the data frame. With an individual
vector, this is easy using select(variable, number, replace = TRUE). I
looked on-line I found some guides to sample from a data frame using
indexing, but I don't seem to be able to get the results that I want.

# Example:
name <- c("andy", "kevin", "lindsay", "karen")
age <- c(29, 37, 26, 31)
gender <- c("M", "M", "F", "F")
people <- data.frame(name, age, gender)

# I would like to create a random sample of my rows, N =10
people[sample(1:nrow(people), 10, replace = TRUE)]

Sometimes this generates an error (undefined columns), and sometimes it
creates a random selection (N=10) of my columns, not my rows. This lead
me to try:

people[sample(1:ncol(people), 10, replace = TRUE)]
#AND
people[sample(people, 10, replace = TRUE)]

Neither works. Could someone please explain to me what I am missing?

I feel like I am close because this does work:

counts <- 1:10
counts[sample(counts, 20, replace = TRUE)]


Thank you
--andy

System Info

    platform       i486-pc-linux-gnu
    arch           i486
    os             linux-gnu
    system         i486, linux-gnu
    status
    major          2
    minor          7.1
    year           2008
    month          06
    day            23
    svn rev        45970
    language       R
    version.string R version 2.7.1 (2008-06-23)

-- 
This is the price and the promise of citizenship.
        -- Barack Obama, 44th President of the United States

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to