R语言 如何平均正确的数据:温度-每天和每个站点的平均温度

uubf1zoe  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(208)

我必须减去每天的平均温度到每一个温度值在白天也考虑到它来自车站,沿着9个月。我已经有了平均值,我的数据是这样的:

  1. >>> df1
  2. Station Date Temperature
  3. 0 Station1 2022-05-1 9:30:00 7,4
  4. 1 Station1 2022-05-1 9:45:00 7,45
  5. 2 Station1 2022-05-1 10:00:00 8,2
  6. 3 Station1 2022-05-1 10:15:00 8,4
  7. 4 Station1 2022-05-1 10:30:00 8,9
  8. 5 Station1 2022-05-1 9:30:00 7,5
  9. 6 Station2 2022-05-1 9:45:00 7,56
  10. 7 Station2 2022-05-1 10:00:00 8,4
  11. 8 Station2 2022-05-1 10:15:00 8,7
  12. 9 Station2 2022-05-1 10:30:00 8,1
  13. 10 ...
  14. >>> df2
  15. Station Date AverageTemperaturePerDayAndStation
  16. 0 Station1 2022-05-1 8
  17. 1 Station1 2022-05-2 8,3
  18. 2 Station1 2022-05-3 8,6
  19. 3 Station1 2022-05-4 8,4
  20. 4 Station1 2022-05-5 7,9
  21. 5 Station2 2022-05-1 6
  22. 6 Station2 2022-05-2 7,3
  23. 7 Station2 2022-05-3 8,6
  24. 8 Station2 2022-05-4 7,4
  25. 9 Station2 2022-05-5 6,9
  26. 10 ...

所以我想用R减去温度-每天和站点的平均温度,就像这样:

  1. >>> df3
  2. Station Date CorrectedTemperature
  3. 0 Station1 2022-05-1 9:30:00 7,4 - 8
  4. 1 Station1 2022-05-1 9:45:00 7,45 - 8
  5. 2 Station1 2022-05-1 10:00:00 8,2 - 8
  6. 3 Station1 2022-05-1 10:15:00 8,4 - 8
  7. 4 Station1 2022-05-1 10:30:00 8,9 - 8
  8. 5 Station1 2022-05-1 9:30:00 7,5 - 8
  9. 6 Station2 2022-05-1 9:45:00 7,56 - 6
  10. 7 Station2 2022-05-1 10:00:00 8,4 - 6
  11. 8 Station2 2022-05-1 10:15:00 8,7 - 6
  12. 9 Station2 2022-05-1 10:30:00 8,1 - 6
  13. 10 ...
xzlaal3s

xzlaal3s1#

我谨提出以下建议:

  1. #generate dataframes df1 and df2
  2. df1 <- data.frame(Station =c(rep("Station1",5), rep("Station2",5)),
  3. Date = c("2022-05-1 9:30:00","2022-05-1 9:45:00","2022-05-1 10:00:00","2022-05-1 10:15:00", "2022-05-1 10:30:00",
  4. "2022-05-1 9:30:00","2022-05-1 9:45:00","2022-05-1 10:00:00","2022-05-1 10:15:00", "2022-05-1 10:30:00"),
  5. Temperature = c(7.4, 7.45, 8.2, 8.4, 8.9, 7.5, 7.56, 8.4, 8.7, 8.1))
  6. df2 <- data.frame(Station=c(rep("Station1",5), rep("Station2",5)),
  7. Date = c("2022-05-01","2022-05-02","2022-05-03","2022-05-04","2022-05-05",
  8. "2022-05-01","2022-05-02","2022-05-03","2022-05-04","2022-05-05"),
  9. AverageTemperaturePerDayAndStation =c(8, 8.3, 8.6, 8.4, 7.9, 6, 7.3, 8.6, 7.4, 6.9))
  10. #save Time and date in separate columns
  11. df1$Time <- format(as.POSIXct(df1$Date), format = "%H:%M:%S")
  12. df1$Date <- as.Date(df1$Date)
  13. #change format of Date in df2 with as.Date
  14. df2$Date <- as.Date(df2$Date)
  15. #use left join (i.e. keep all entries from the first dataframe) and join by both Date and Station
  16. df3 <- dplyr::left_join(df1, df2, by =c("Date","Station"))

然后,可以计算新列CorrectedTemperature。

  1. df3$CorrectedTemperature <- df3$Temperature - df3$AverageTemperaturePerDayAndStation

希望这能帮上忙。

展开查看全部

相关问题