-1

I have a big dataset with 1200 rows, which looks like this:

Date CAL_CALFIXO FIXOCAL
2015-07-20 Yes No
2015-07-21 Yes No
2015-07-22 No No
2015-07-23 No Yes
2015-07-24 No No
2015-07-25 Yes Yes
2015-07-26 No No
.......... ........... .......

The Date is in Date format.

My question is the following: how to extract the dates where CAL_CALFIXO and FIXOCAL are "Yes"? The code can have both columns CAL_CALFIXO and FIXOCAL together or separate.

My desired output would look like this (where at least one variable is "Yes"):

Date CAL_CALFIXO FIXOCAL
2015-07-20 Yes No
2015-07-21 Yes No
2015-07-23 No Yes
2015-07-24 No Yes
2015-07-25 Yes Yes
.......... ........... .......

Any help will be much appreciated.

Cláudio Siva
  • 502
  • 2
  • 10
  • pls dont post pictures of data, its not reproducible. See this [post on creating a minimal reprex](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) Now coming to your Q. its pretty simple answer, use dplyr verbs like `group_by()` for grouping the categorical vars and then use `filter()` for filtering the date. – mnm May 19 '21 at 14:42
  • that's not a picture, is a table created in the question section, but thanks I'll improve. thanks for the reply, appreciate it. – Cláudio Siva May 19 '21 at 14:50

1 Answers1

0

When you post an image with your data is a little difficult understand the structure of your data. You can review this link https://stackoverflow.com/help/how-to-ask.

Using your data and the library tidyverse I believe that I have your answer.

library(tidyverse)

df<-data.frame(Date=seq(as.Date("2015-07-20"), as.Date("2015-07-26"), by="day"),
           CAL_CALFIXO=c("yes","yes","no", "no", "no","yes", "no" ),
           FIXOCAL=c("no","no","no","yes","no","yes","no")) # The DF with your data

Date<-df%>%filter(.,  CAL_CALFIXO=="yes"&FIXOCAL=="yes")%>% #Filter extract the columns with the desired characteristics
  select(., Date)# with Select I am choosing to save only the Column Date in a new Dataframe.

My output is :

>Date
        Date
1 2015-07-25
Eve Chanatasig
  • 397
  • 2
  • 10
  • thanks for the reply. The code does not work, it gives an empty table. I'm trying to change the arguments as well, but no success. – Cláudio Siva May 19 '21 at 15:03
  • You can run this code dput(head(YOUR_DATAFRAME,10)) in order to share us the structure of your data base. – Eve Chanatasig May 19 '21 at 15:07
  • I've use the code that Eve mentioned: > dput(head(NDVI,10)) structure(list(Date = structure(c(16644, 16645, 16646, 16647, 16648, 16649, 16650, 16651, 16652, 16653), class = "Date"), CAL_CALFIXO = c("Yes", "No", "No", "No", "No", "No", "No", "No", "No", "No"), FIXOCAL = c("No", "No", "No", "No", "No", "No", "No", "No", "No", "No")), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")) – Cláudio Siva May 19 '21 at 15:28
  • @CláudioSiva I use your dataframe with my code and it functions. Obviously with the firsts 10 observations the table is empty because any observation meet the criteria. Maybe your question is being misunderstood. You can provide your desired output. – Eve Chanatasig May 19 '21 at 15:43
  • I've posted in the question section the desired output. Thanks for the understanding, I'm quite new to R and now I'm working with this dataset which has more than 3 years and some of the past configurations are totally unknown to me. – Cláudio Siva May 19 '21 at 16:05
  • ok, I understood. I provided a code which use the logical argument CAL_CALFIXO and FIXOCAL == "yes", but in your case the code will be: Date<-df%>%filter(., CAL_CALFIXO=="yes" | FIXOCAL=="yes") In your case the logical argument is or (|). – Eve Chanatasig May 19 '21 at 16:10
  • That's exactly what I needed. Thank you so much for your help. – Cláudio Siva May 19 '21 at 16:20