-1

First of all, I am using the ukpolice package. We needed to select data from a specific city for 5 months, this dataframe is called crimes.

Now, we need to create a new dataframe that contains the columns: category, month and number.

The columns category and month already exist, but the column number still needs to be created. We are supposed to do this using the dplyr functions.

I created a frequency table using:

number_crime_type <- table(crimes$category)

In this table, the frequency per crime is established.

But I don't understand how I can create a new column in which the number of the crime is mentioned.

I'm really a rookie with R, so I really don't understand a lot of it.

How can I create a column that entails the number of the crime and add this column to the new dataframe?

Jonas
  • 1,760
  • 1
  • 3
  • 12

1 Answers1

1

Nikki,

If you don't already have it, install the tidyverse package by: install.packages('tidyverse') then load the package library('tidyverse'). This will ensure that you have the dplyr package.

If the count is a column in the crimes dataset you can do this:

new_dataframe <- crimes %>% select(category, count)

If you need to calculate the count based on the frequency of a given category, do this:

new_dataframe <- crimes %>% select(category) %>% group_by(category) %>% summarize('count' = n())

Good luck!

C Jeruzal
  • 538
  • 2
  • 10