DUMMY VARIABLE - how to create one?

Hi there,

I need to create a dummy variable where I set my values "Monthly_Total_Returns" equal to 1 in March 2020. 0 otherwise. The data is all aligned and organized. My variables' names (columns) are "Monthly_Total_Returns, where I find my values", "date, where there are my dates (month + year. e.g. "2020-03") of the years. Can you help me with that? This is what I have done so far.

Data$Monthly_Total_Returns
Monthly_Total_Returns_bad_march <- Data$Monthly_Total_Returns

Monthly_Total_Returns_bad_march %>%
filter(format(Date, format = "%Y-%m") == "2020-03", Data$Monthly_Total_Returns)

Monthly_Total_Returns_bad_march <- 1

My boss told me that I don't need to use the FILTER function and it should be easier and more straightforward. Please let me know.

Can you please share a small part of the data set in a copy-paste friendly format?

In case you don't know how to do it, there are many options, which include:

  1. If you have stored the data set in some R object, dput function is very handy.

  2. In case the data set is in a spreadsheet, check out the datapasta package. Take a look at this link.

While you will probably get a more complete answer if you follow @andresrcs's suggstion, perhaps something like

Monthly_Total_Returns_bad_march <- ifelse(format(Date, format = "%Y-%m") == "2020-03",1,0)

The variables that I need to use are: "Data_Date_Security_Monthly" and "Monthly_Total_Returns"

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.