Ananya Vidyanathan
Ananya Vidyanathan

Reputation: 51

How to extract a part of Data frame from the whole?

My dataframe 'y'

 steps date       interval
   <dbl> <date>        <int>
 1     0 2012-10-01        0
 2     0 2012-10-01        5
 3     0 2012-10-01       10
 4     0 2012-10-01       15
 5     0 2012-10-01       20
 6     0 2012-10-01       25
 7     0 2012-10-01       30
 8     0 2012-10-01       35
 9     0 2012-10-01       40
10     0 2012-10-01       45
# ... with 17,558 more rows

I want to extract rows where month==11 & day==1 with columns of "steps" and "interval" only.

NOT WORKING CODE

y[month(y$date)==11 & day(y$date)==1,c(y$steps,y$interval)]

I also tried using a combination of using the first select and then filter, but while we select() the specified columns "steps" and "interval", I am unable to apply condition in Filter() as it depends on removed column "date".

Can someone explain to me an easy way of resolving this issue?

Upvotes: 2

Views: 343

Answers (3)

akrun
akrun

Reputation: 887118

Another approach is to remove the 'year' part with str_remove, filter and select the columns

library(dplyr)
library(stringr)
Y %>% 
  filter(str_remove(date, "^\\d{4}-") == '11-01') %>%
  select(steps, interval)

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388982

Using base R we can format the date use subset and select columns

subset(df, format(date, "%d-%m") == "01-11", select = c(steps, interval))

Upvotes: 2

neilfws
neilfws

Reputation: 33782

I assume month() and day() are from lubridate. This operation is easier using dplyr::filter and dplyr::select.

Something like this should work (difficult to test without better example data):

library(dplyr)
library(lubridate)

y %>% filter(month(date) == 11,
             day(date) == 1) %>%
  select(steps, interval)

Upvotes: 2

Related Questions