简体   繁体   中英

How to select random samples in a dataframe?

I am using the following command to create 50 random samples in a dataset. and I want to know the mean of observations(area).

ds1 %>%
  sample_n(size = 50) %>%
  summarise(x_bar = mean(area))

But I get

Error in function_list[[i]](value) : could not find function "sample_n"

I tried searching for the function sample_n using getAnywhere() but I didn't find the object.

Instead it works when I use,

ds1_samp3 <- 
  ames[sample(nrow(ds1), 1000), ]

ds1_samp3 %>% 
  summarise(mu = mean(area))

Just want to know why the first command doesn't work?

Thanks, Vkva

sample_n is contained in the dplyr package. It will work as long as you have installed & imported said package in your session. Substituting with sample (from base) will not work because it does not assume data.frame input, whereas sample_n does.

sample_n is not a function in R. Use sample instead:

ds1 %>%
  sample(size = 50) %>%
  summarise(x_bar = mean(area))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM