how to count the no of similar rows in a dataframe and classify them.

I have a data frame in which I have columns start_city and end_city. I want to create another data frame that would contain city name and count of city being the start_city and end_city. How do I go about this?

library(tidyverse)

(exampl_1 <- data.frame(
  start_city = rep(letters[1:3], 2),
  end_city = letters[2:7]
))

(result <-
  pivot_longer(exampl_1,
    cols = everything()
  ) |>
  group_by(value) |>
  count(name) |>
  pivot_wider(
    names_from = "name",
    values_from = "n"
  ))

This topic was automatically closed 42 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.