To put several value in data frame

Hello! I been stuck with this for like 6 hours and I know it’s something simple but I don’t know what I am missing. I made a data frame and in that data frame it contains 10 Cities and my assignment was to put wether temperature for a month for each city. My problem is to put the right weather temperature to the right city. I can’t find a way to put 30 different numbers to a single city.
I will be grateful for any tips or if somebody can link me a YouTube link for explanation!

I made several variables that contain 30 different numbers but I can’t connect them to the right city

Can you paste the output? dput(head(YOURDATAFRAME, 20)

#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]]

get_maximum <- function(vector){
maximum <- vector[[1]]
for (i in seq_along(vector)){
if (vector[[i]] > maximum) maximum <- vector[[i]]

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.

Does this create a data.frame you can use?

city <- c("Stockholm", "Göteborg", "Piteå", "Gävle", "Borås", "Malmö", "Uppsala", "Umeå", "Skövde", "Helsingborg")

dat2 <- data.frame(matrix(floor(runif(300, min=16, max=30)), nrow = 10))
 dat1  <-    cbind(city, dat2)     

thank you for your answer, i was searching all day yesterday on how to put the city in the row! but my functions dosent work, it says "Error in unique.default(x, nmax = nmax) :
unique() applies only to vectors

tapply(försök4$Temp, försök4$stad, get_minimum)
Borås Gävle Göteborg Helsingborg Jönkping
16 26 22 23 19
Malmö Skövde Stockholm Umeå Uppsala
17 18 23 25 28
why didnt it work? i didnt get the minimum ?
i changed the name of the column to temp

We need to see what your data looks like. A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need.

The error seems to indicate that your data is not in the form you think it is. dput() will give us an exact copy of your data.

i manage to find a solution to my problem, i want to thank u for your help!!

Ah good. What was is it?
Would you mark it as solved?

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