Selecting rows from a data

I have a data of historical prices of a company stored in a .csv file. I have imported it into Rstudio.
Now I have to select the first five dates of each month alongwith the closing price for 15 year period. Kindly help me with the code.

You could use the group_by function to group by month and then the arrange function to sort closing prices and then the head function with n = 5 to choose the 5 dates.

data %>%
   group_by(month) %>%
   arrange(closing_price) %>%
   head(n = 5) %>%

Now, I am getting the data with dates as on 1st for the initial five years. However, I want the Dates 1 to 5 for all the months coming in 15 year period.

Try grouping it by both the month and year variables.

Result is the same as before.

How is your data set up?

1st column is Year
2nd cloumn is Month
3rd column is Date in dd/mm/yy
4th column is Closing price

Is your month column numeric(01, 02, ..) or character(January, February, March...)?

numerically listed,

Okay, this uses the ddply function from the plyr package.


data %>%
   ddply(.(year, month), head, n = t)

Hi @goldbiggod! Welcome to RStudio Community!

Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

This includes a toy data set that mimics your actual data, the code that you have tried, and the error that you are getting. Thanks!