0

I am using R for a project for University. I imported a csv file and created a df. Everything was going smoothly until I had to gather the percentages of age groups in the "Age" column. There are 3,000 rows of information in my df. How do I only sample information from rows 50-200 to find the percentages of people ages 15-20, 21-25, 26-30, and 31-35?

Phil
  • 7,287
  • 3
  • 36
  • 66
S_C
  • 1
  • 1
  • 2
    Hello, can you post an example showing the structure of your data frame? It will be easier to help. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Rosalie Bruel Sep 02 '21 at 07:47
  • 1
    If your question is "how to sample the rows 50 to 200, then, assuming your df is called df in the environment: `df[50:200, ]` in base R, or using the package dplyr, `df %>% slice(50:200)` – Rosalie Bruel Sep 02 '21 at 07:50
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 02 '21 at 12:39
  • You can sample the row indices like so (this will only sample one row): `df[sample(20:500, 1),]`. If you need to sample with replacement then `sample` has a `replace` argument. – LMc Sep 02 '21 at 14:13
  • use `dplyr::between` to filter people between certain age. – Nad Pat Sep 04 '21 at 06:24

2 Answers2

0

You can try creating another df which only takes information from rows 50-200 using the slice function e.g my_data %>% slice(1:6) would give rows 1-6 I believe. Incase you didnt know, this function exists in tidyverse, which you can call using library(tidyverse). For filtering by particular age groups, you can again use the tidyverse filter function, e.g my_data %>% filter.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

If your goal is to sample, better than slice specific rows you can use the function sample_n

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32