library(dslabs)
data("murders")
df<-murders
pop <- df$population
pop <- sort(pop)
pop[1]
## [1] 563626
population_data <- data.frame(
country = c("Country A", "Country B", "Country C", "Country D"),
population = c(1000000, 500000, 200000, 3000000)
)
pop <- population_data$population
index_of_smallest <- order(pop)[1]
index_of_smallest
## [1] 3
which.min(population_data$population)
## [1] 3
states <- murders$state
# Get the state with the smallest population size
state_with_smallest_population <- states[index_of_smallest]
state_with_smallest_population
## [1] "Arizona"
murders <- data.frame(
state = c("State A", "State B", "State C", "State D"),
population = c(1000000, 500000, 2000000, 300000)
)
ranks <- rank(murders$population)
my_df <- data.frame(state = murders$state, rank = ranks)
my_df
## state rank
## 1 State A 3
## 2 State B 2
## 3 State C 4
## 4 State D 1
ranks <- rank(murders$population)
my_df <- data.frame(state = murders$state, rank = ranks)
ind <- order(murders$population)
my_df <- my_df[ind, ]
my_df
## state rank
## 4 State D 1
## 2 State B 2
## 1 State A 3
## 3 State C 4
ind <- is.na(na_example)
num_nas <- sum(ind)
num_nas
## [1] 145
average_non_na <- mean(na_example[!is.na(na_example)])
average_non_na
## [1] 2.301754
# Original temperature data in Fahrenheit
temp <- c(35, 88, 42, 84, 81, 30)
city <- c("Beijing", "Lagos", "Paris", "Rio de Janeiro", "San Juan", "Toronto")
# Convert Fahrenheit to Celsius
temp_celsius <- (5/9) * (temp - 32)
# Create the data frame with city names and both Fahrenheit and Celsius temperatures
city_temps <- data.frame(name = city, temperature_fahrenheit = temp, temperature_celsius = temp_celsius)
# Resulting data frame
city_temps
## name temperature_fahrenheit temperature_celsius
## 1 Beijing 35 1.666667
## 2 Lagos 88 31.111111
## 3 Paris 42 5.555556
## 4 Rio de Janeiro 84 28.888889
## 5 San Juan 81 27.222222
## 6 Toronto 30 -1.111111
# Initialize the sum
sum_value <- 0
# Calculate the sum using the provided series
for (n in seq(22, 1002, by = 10)) {
sum_value <- sum_value + 1/n^2
}
# Print the result
sum_value
## [1] 0.005630024
# Sample data (replace this with your actual data)
murders <- data.frame(
state = c("State A", "State B", "State C"),
murders = c(100, 50, 75),
population = c(500000, 1000000, 750000)
)
# Calculate murder rate per 100,000 people for each state
murder_rate <- (murders$murders / murders$population) * 100000
# Compute the average murder rate for the US using mean function
average_murder_rate <- mean(murder_rate)
# Print the average murder rate
average_murder_rate
## [1] 11.66667