Need help sorting by month in R!

Hi,

I am trying to sort these dates so that it groups all the same months together, but I am really struggling to do so! I would like to try this using an if-else loop Below is an example of what I kind of need the output to look like.

01 who was CEO
01
01
02
02
etc...

Here is the code I have:

rm(list = ls())

setwd("~/Desktop/researchprofpopper")


library(tidyverse)
library(sandwich)
library(stargazer)
library(ggplot2)
library(dplyr)
library(lubridate)

options(scipen = 9) 
cse = function(reg) {
  rob = sqrt(diag(vcovHC(reg, type = "HC1")))
  return(rob)
}

prac08 <- read.csv("prac08.csv", header=TRUE, sep=",")
prac08 <- prac08 %>%
  mutate(DateStartRole=ifelse(DateStartRole=="N", NA, DateStartRole)) %>%
  mutate(DateStartRole = as.Date(DateStartRole, "%Y%m%d"))

df %>%
  mutate(month = format(date, "%m"), year = format(date, "%Y")) %>%
  group_by(month, year) %>%
  summarise(total = sum(value))

You want to use this instead: ... %>% mutate(DateStartRole = ymd(DateStartRole))

And you can use ... %>% arrange(desc(month(DateStartRole))) or ... %>% arrange(-desc(month(DateStartRole))) for ordering by month

I think this problem might be a little more complicated than you think. If you simply sort by date, then you'll still have a data frame with a row for each unique combination of start date and end date. What I think you want is a data frame where each row is a calendar month. In that case, you should create a new data frame with a column for incrementing month, and apply logic between that calendar month and the CEO start and end dates to populate columns for each company with values for CEO name.

Would I do something like:

prac08$month etc...?

Someone else told me that an if else loop would be best?

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:


Short Version

You can share your data in a forum friendly way by passing the data to share to the dput() function.
If your data is too large you can use standard methods to reduce it before sending to dput().
When you come to share the dput() text that represents your data, please be sure to format your post with triple backticks on the line before your code begins to format it appropriately.

```
( example_df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
5, 4.4, 4.9), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9, 3.4, 
3.4, 2.9, 3.1), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 
1.4, 1.5, 1.4, 1.5), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 
0.4, 0.3, 0.2, 0.2, 0.1), Species = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("setosa", "versicolor", "virginica"
), class = "factor")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame")))
```

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.