Selecting rows from a data


#1

Hi,
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.


#2

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) %>%
   ungroup()

#3

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.


#4

Try grouping it by both the month and year variables.


#5

Result is the same as before.


#6

How is your data set up?


#7

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


#8

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


#9

numerically listed,
1
2
3
.
.
.


#10

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

library(plyr)

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

#11

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!