#So my assignment is to get input temperature of 10 cities for a month. they want me to create 3 function to compute the minimum maximum and average temperature of each city in that month using manual calculations and the in-built functions in R
functions to calculate min, max and mean
get_minimum <- function(vector){
minimum <- vector[[1]]
for (i in seq_along(vector)){
if (vector[[i]] < minimum) minimum <- vector[[i]]
}
minimum
}
get_maximum <- function(vector){
maximum <- vector[[1]]
for (i in seq_along(vector)){
if (vector[[i]] > maximum) maximum <- vector[[i]]
}
maximum
}
get_mean <- function(vector){
sum(vector) / length(vector)
}
#Then i put every city that i need in this variable
City <- c("Stockholm", "Göteborg", "Piteå", "Gävle", "Borås", "Malmö", "Uppsala", "Umeå", "Skövde", "Helsingborg")
##then i create a value for the temp
temp_stockholm <- floor(runif(30, min=16, max=30))
temp_göteborg <- floor(runif(30, min=16, max=30))
temp_piteå <- floor(runif(30, min=16, max=30))
temp_gävle <- floor(runif(30, min=16, max=30))
temp_borås <- floor(runif(30, min=16, max=30))
temp_malmö <- floor(runif(30, min=16, max=30))
temp_uppsala <- floor(runif(30, min=16, max=30))
temp_umeå <- floor(runif(30, min=16, max=30))
temp_skövde <- floor(runif(30, min=16, max=30))
temp_helsingborg <- floor(runif(30, min=16, max=30))
its in this moment i hit the wall.. i want to create a data frame but i dont know how to put in every temp.
data <- data.frame( City = City)
##how do i put my temps in now? I am stuck and cant come around it, it made my try it another way. But that didnt go as planned either. When i wanted the max, min and mean from every city, it showed me only one. It went like this:
Stockholm <- temp_stockholm
Göteborg <- temp_göteborg
Piteå <- temp_piteå
Gävle <- temp_gävle
Borås<- temp_borås
Malmö <- temp_malmö
Uppsala <- temp_uppsala
Umeå <- temp_umeå
Skövde <- temp_skövde
Helsingborg <- temp_helsingborg
data <- data.frame(Borås = c(temp_borås),
Gävle = temp_gävle,
Göteborg = temp_göteborg,
Piteå = temp_helsingborg,
Jönköping = temp_jönköping,
Malmö = temp_malmö,
Skövde = temp_skövde,
Stockholm = temp_stockholm,
Umeå = temp_umeå,
Uppsala = temp_uppsala)
#when i do like this and ask for min max and mean, it only show my 1 result.