The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(lubridate)
Attaching package: 'lubridate'
The following objects are masked from 'package:base':
date, intersect, setdiff, union
Chicago_small <-"/Users/chenwanhua/Desktop/NEOMA_DataProgramming-main/datasets-master/Chicago_small.xlsx"chicago_data <-read_excel(Chicago_small)# Display the first 10 rows and 5 columnshead(chicago_data[, 1:5], 10)
# A tibble: 10 × 5
ID Date `Primary Type` Description `Location Description`
<dbl> <chr> <chr> <chr> <chr>
1 9471292 01/23/2014 07:40:… MOTOR VEHICLE… AUTOMOBILE STREET
2 10304260 11/07/2015 10:00:… THEFT OVER $500 SIDEWALK
3 10689471 09/21/2016 01:30:… BATTERY DOMESTIC B… RESIDENCE
4 10497936 04/25/2016 08:25:… BATTERY DOMESTIC B… RESIDENCE
5 9863005 11/19/2014 04:04:… THEFT RETAIL THE… SMALL RETAIL STORE
6 9274420 08/22/2013 12:15:… CRIMINAL DAMA… TO VEHICLE CHA PARKING LOT/GROUN…
7 8573204 04/18/2012 08:20:… THEFT FROM BUILD… COMMERCIAL / BUSINESS…
8 10001509 03/19/2015 12:30:… BATTERY DOMESTIC B… SIDEWALK
9 9624312 05/24/2014 03:20:… BATTERY AGGRAVATED… APARTMENT
10 10269064 10/09/2015 10:30:… THEFT FROM BUILD… BAR OR TAVERN
# Displays the number of rows and columns of the data framedim(chicago_data)
[1] 200 11
# Extract the year from the 'Date' column and create a 'Year' columnchicago_data <- chicago_data %>%mutate(Year =year(mdy_hms(Date, truncated =3))) %>%rename(Home = Domestic) %>%select(-Ward, -ID, everything()) # Verify that both 'Year' and 'Primary Type' columns are present in the data frameif("Year"%in%names(chicago_data) &&"Primary Type"%in%names(chicago_data)) { chicago_data$Year <-as.factor(chicago_data$Year)# Use the 'Primary Type' and 'Year' columns to calculate the number of crimes per year crimes_per_year <-tapply(chicago_data$`Primary Type`, chicago_data$Year, FUN = length)print(crimes_per_year)} else {stop("One or both of the columns 'Year' or 'Primary Type' do not exist.")}
2012 2013 2014 2015 2016 2017
31 52 28 49 39 1
Mission 2
library(readxl)library(dplyr)library(lubridate)data_path <-"/Users/chenwanhua/Desktop/NEOMA_DataProgramming-main/datasets-master/Chicago_small.xlsx"chicago_data <-read_excel(data_path)# Convert the string in the 'Date' column to a datetime objectchicago_data <- chicago_data %>%mutate(DateTime =mdy_hms(Date, tz ="UTC")) %>%mutate(Date =as.Date(DateTime),Time =format(DateTime, "%H:%M"),Hour =hour(DateTime)) %>%select(-Ward, -ID, everything()) %>%rename(Home = Domestic)# Calculate crimes by hourcrimes_by_hour <-table(chicago_data$Hour)# Find the most dangerous hour most_dangerous_hour <-which.max(crimes_by_hour)cat("most_dangerous_hour:", names(most_dangerous_hour), ",has", crimes_by_hour[most_dangerous_hour], "crimes.\n")
most_dangerous_hour: 13 ,has 17 crimes.
Mission 3
# Define a function to calculate the average number of vowels per input stringAverageVowels <-function(input_string) {if (!is.character(input_string)) {stop("The input parameter must be a string.") } vowels <-c('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U') vowel_count <-0for (char instrsplit(input_string, "")[[1]]) {if (char %in% vowels) { vowel_count <- vowel_count +1 } }# Calculate the average, rounded to 2 decimal places average <-round(vowel_count /nchar(input_string), 2)return(average)}# Test the function with example stringstest_strings <-c("John", "Jim", "Eugene")sapply(test_strings, AverageVowels)
John Jim Eugene
0.25 0.33 0.67
Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
1+1
[1] 2
You can add options to executable code like this
[1] 4
The echo: false option disables the printing of code (only output is displayed).