Calculate Anomaly in Temperature


#1

Hi Guys,
I made a list in R of length 5 (each element represents a different city): inside each element there are 31 temperatures.
I called that list "Enero" (January in spanish).
In order to calculate the mean for each element of my list (the mean for January Temperature for each city) I used the sentence:
mediaenero= lapply(enero, mean) and it worked!

Now, I need to calculate the difference between each temperature from each city and the mean (for each city).
How could I do that?

The only thing I have is:

  1. A list called "Enero" with 5 elements (each one contains 31 temperatures)
  2. A list called "mediaenero" with 5 elements (the mean of all this 31 temperatures).

#2

Hi there - welcome to community.rstudio.com! Can you please show the code for what you've tried so far to find these differences? I ask because it sounds like this might be a homework question. If it is, please see our homework policy.


#3

rm(list=ls())

#loadthedata
estaciones=load("t_media_EF.RData)
enero = estaciones[,2]
mediaenero=lapply(enero, mean)

#I can`t find a function to make the difference between the temperatures in ENERO and the mean
calculated in MEDIAENERO


#4

I think it would be helpful to see the data your working with in a reprex.

list in R of length 5 (each element represents a different city): inside each element there are 31 temperatures.

If you data is small enough (and pls make it smaller if you're dealing with a large dataset), you can do this with the dput function, for example,

ex_list <- list(
  city_a = data.frame(
    date = 1:3,
    temp = c(4,12,4)
  ),
  city_b = data.frame(
    date = 1:3,
    temp = c(5,7,2)
  )
)

dput(ex_list)
#> list(city_a = structure(list(date = 1:3, temp = c(4, 12, 4)), class = "data.frame", row.names = c(NA, 
#> -3L)), city_b = structure(list(date = 1:3, temp = c(5, 7, 2)), class = "data.frame", row.names = c(NA, 
#> -3L)))

Created on 2018-09-17 by the reprex package (v0.2.0.9000).

Note that the final list of code there replicate ex_list,

ex_list <- list(city_a = structure(list(date = 1:3, temp = c(4, 12, 4)), class = "data.frame", row.names = c(NA, 
-3L)), city_b = structure(list(date = 1:3, temp = c(5, 7, 2)), class = "data.frame", row.names = c(NA, 
-3L)))

And here's a handy guide to making a reprex (FAQ: What's a reproducible example (`reprex`) and how do I do one?)