Random sample of rows from subset of an R dataframe [duplicate]
问题 This question already has answers here : Sample random rows in dataframe (10 answers) Closed 6 years ago . Is there a good way of getting a sample of rows from part of a dataframe? If I just have data such as gender <- c("F", "M", "M", "F", "F", "M", "F", "F") age <- c(23, 25, 27, 29, 31, 33, 35, 37) then I can easily sample the ages of three of the Fs with sample(age[gender == "F"], 3) and get something like [1] 31 35 29 but if I turn this data into a dataframe mydf <- data.frame(gender, age