-1

I apologize for publishing this post again, but I made a series of errors in the previous post.

My data has precipitation measurements per minute. When I calculate the hourly averages, I would like to calculate this as the sum of the rain minutes (expressed in hours) when the precipitation is >0.

Help is very appreciated. Thank you in advance:

    > metsacbo[1021:1085,]
         date               press  temp  ws wd   u%  precipitation 
    1021 2017-06-06 17:00:00 963.9 19.6  2 344.3 55.6    0.00           
    1022 2017-06-06 17:01:00 980.2 19.9  2 348.3 56.0    0.00           
    1023 2017-06-06 17:02:00 980.2 19.9  2 355.4 55.8    0.00           
    1024 2017-06-06 17:03:00 980.2 19.8  2 351.8 54.0    0.00           
    1025 2017-06-06 17:04:00 980.1 19.8  2 354.4 54.2    0.00           
    1026 2017-06-06 17:05:00 963.8 19.5  2   2.8 54.1    0.00           
    1027 2017-06-06 17:06:00 980.1 19.8  3 345.3 54.7    0.00           
    1028 2017-06-06 17:07:00 980.0 19.7  2 345.8 54.8    0.00           
    1029 2017-06-06 17:08:00 979.9 19.7  2 352.5 55.1    0.00           
    1030 2017-06-06 17:09:00 979.9 19.7  2 355.8 55.9    0.00           
    1031 2017-06-06 17:10:00 979.9 19.7  2   1.8 56.3    0.00           
    1032 2017-06-06 17:11:00 980.0 19.6  2   3.7 56.1    0.00           
    1033 2017-06-06 17:12:00 980.0 19.6  2 354.9 56.0    0.00           
    1034 2017-06-06 17:13:00 980.0 19.6  2 357.1 56.6    0.00           
    1035 2017-06-06 17:14:00 979.9 19.6  2 359.9 57.1    0.00           
    1036 2017-06-06 17:15:00 979.9 19.6  2   3.4 57.5    0.00           
    1037 2017-06-06 17:16:00 979.8 19.6  2 340.0 58.1    0.00           
    1038 2017-06-06 17:17:00 979.8 19.6  3 355.4 58.0    0.00           
    1039 2017-06-06 17:18:00 979.8 19.6  2 336.4 58.0    0.00           
    1040 2017-06-06 17:19:00 979.9 19.6  2 337.9 58.0    0.00           
    1041 2017-06-06 17:20:00 980.0 19.6  3 336.9 57.5    0.02           
    1042 2017-06-06 17:21:00 980.2 19.6  2 334.2 56.3    0.00           
    1043 2017-06-06 17:22:00 980.2 19.5  2 347.3 55.8    0.00           
    1044 2017-06-06 17:23:00 980.2 19.5  2 354.7 56.3    0.00           
    1045 2017-06-06 17:24:00 980.1 19.5  2 348.0 56.7    0.00           
    1046 2017-06-06 17:25:00 979.9 19.5  1   2.7 57.1    0.00           
    1047 2017-06-06 17:26:00 979.8 19.4  3 357.3 57.0    0.00           
    1048 2017-06-06 17:27:00 963.4 19.1  3   1.6 55.8    0.01           
    1049 2017-06-06 17:28:00 963.3 19.0  2   8.4 56.7    0.01           
    1050 2017-06-06 17:29:00 963.3 19.0  2 358.8 57.2    0.00           
    1051 2017-06-06 17:30:00 979.6 19.2  2 352.1 58.0    0.00           
    1052 2017-06-06 17:31:00 979.6 19.2  2 344.1 59.1    0.05           
    1053 2017-06-06 17:32:00 979.6 19.2  3 343.3 60.3    0.23           
    1054 2017-06-06 17:33:00 979.7 19.1  3 346.3 59.9    0.40           
    1055 2017-06-06 17:34:00 979.8 18.9  4 343.1 60.6    0.67           
    1056 2017-06-06 17:35:00 979.9 18.6  3 347.2 62.6    0.52           
    1057 2017-06-06 17:36:00 980.0 18.4  3 340.5 64.7    0.53           
    1058 2017-06-06 17:37:00 980.0 18.1  3 337.5 65.8    0.90           
    1059 2017-06-06 17:38:00 980.2 17.9  2 309.4 67.3    0.51           
    1060 2017-06-06 17:39:00 980.4 17.6  2 321.1 68.9    0.74          
    1061 2017-06-06 17:40:00 980.6 17.3  2 306.1 69.4    0.70           
    1062 2017-06-06 17:41:00 980.8 17.0  2 252.6 70.2    1.29           
    1063 2017-06-06 17:42:00 981.0 16.7  2 226.8 71.2    1.60           
    1064 2017-06-06 17:43:00 981.2 16.4  2 260.7 72.1    1.26           
    1065 2017-06-06 17:44:00 981.1 16.1  2 293.0 73.1    1.77           
    1066 2017-06-06 17:45:00 980.7 15.8  2 324.6 73.7    1.88           
    1067 2017-06-06 17:46:00 980.4 15.5  3 348.7 74.5    1.21           
    1068 2017-06-06 17:47:00 963.7 15.0  2 351.4 74.6    1.03           
    1069 2017-06-06 17:48:00 980.0 15.1  3 329.3 77.5    0.93                   
    1070 2017-06-06 17:49:00 980.0 15.0  1 352.5 78.6    0.71           
    1071 2017-06-06 17:50:00 979.9 15.0  2 343.7 79.4    0.53           
    1072 2017-06-06 17:51:00 979.9 15.0  2 334.1 79.8    0.12           
    1073 2017-06-06 17:52:00 979.9 15.1  2 358.4 80.1    0.05          
    1074 2017-06-06 17:53:00 979.9 15.1  2 345.6 80.0    0.01           
    1075 2017-06-06 17:54:00 979.9 15.2  2   0.4 79.4    0.01          
    1076 2017-06-06 17:55:00 979.8 15.3  2 354.4 78.7    0.00           
    1077 2017-06-06 17:56:00 979.7 15.4  2 355.8 78.1    0.00           
    1078 2017-06-06 17:57:00 979.7 15.5  1   8.9 77.8    0.00           
    1079 2017-06-06 17:58:00 979.7 15.6  1  32.4 77.4    0.00           
    1080 2017-06-06 17:59:00 979.7 15.7  1  49.5 77.0    0.00           
    1081 2017-06-06 18:00:00 979.8 15.8  1  53.1 76.8    0.00           
    1082 2017-06-06 18:01:00 979.8 15.8  1  34.6 77.0    0.00           
    1083 2017-06-06 18:02:00 979.9 15.9  2  35.0 76.8    0.00           
    1084 2017-06-06 18:03:00 979.9 15.9  2  10.0 76.4    0.00           
    1085 2017-06-06 18:04:00 980.0 15.9  2   3.2 76.5    0.00 

This is the result I would like to have, when I calculate the hourly averages.

In the "time prec" column I would like to have the sum of the minutes of the rain events for each hour.

head(dmeth, 20)

A tibble: 20 x 8

date press temp ws wd u. prec. time prec.(h) 1 2017-06-06 00:00:00 984. 17.8 0.783 15.3 72.8 0. 0 2 2017-06-06 01:00:00 979. 17.5 0.550 32.1 77.0 0. 0 3 2017-06-06 02:00:00 982. 17.5 0.800 0.567 74.9 0. 0 4 2017-06-06 03:00:00 982. 17.5 0.717 358. 78.1 0. 0 5 2017-06-06 04:00:00 982. 17.7 0.583 40.7 73.7 0. 0 6 2017-06-06 05:00:00 981. 17.6 0.767 354. 76.1 0. 0 7 2017-06-06 06:00:00 980. 17.8 0.700 6.19 74.2 0. 0 8 2017-06-06 07:00:00 981. 18.1 0.683 26.8 73.3 0. 0 9 2017-06-06 08:00:00 981. 18.8 0.800 26.4 69.5 0. 0 10 2017-06-06 09:00:00 981. 19.1 0.100 45.9 72.4 0. 0 11 2017-06-06 10:00:00 979. 19.4 0.317 351. 70.4 0. 0 12 2017-06-06 11:00:00 981. 20.0 1.20 41.3 67.2 0.120 0.03 13 2017-06-06 12:00:00 980. 21.4 1.58 121. 58.2 0. 0 14 2017-06-06 13:00:00 979. 22.0 1.98 123. 59.2 0. 0 15 2017-06-06 14:00:00 979. 21.8 1.78 120. 59.8 0. 0 16 2017-06-06 15:00:00 978. 20.0 1.55 122. 69.8 8.15 0.43 17 2017-06-06 16:00:00 979. 20.4 1.68 3.33 64.9 0. 0 18 2017-06-06 17:00:00 978. 18.1 2.15 345. 64.0 17.7 0.45 19 2017-06-06 18:00:00 978. 16.8 2.07 15.2 74.4 0. 0 20 2017-06-06 19:00:00 980. 17.0 2.73 357. 71.4 6.18 0.52

cerzza
  • 1
  • 1
  • 1
    are we supposed to enter the data ourself ? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example any attempt from your part ? – timat Mar 28 '18 at 10:17
  • Possible duplicate of [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Milad Rashidi Mar 28 '18 at 10:18
  • it would generally help to provide data in a way that others can easily copy. an example of the expected output would make things clearer too, especially in the absence of any code example / attempt – RolandASc Mar 28 '18 at 12:30
  • @cerzo I'm still not clear on what you're looking for. You can update your question with an edit showing your expected result (don't add it in an answer), but if it is a column full of 0s, that doesn't help us understand which rows should have a value and what that value should be. – De Novo Mar 28 '18 at 17:54
  • I apologize for the banality, but I can not find the link to add information to my question. Could someone tell me? – cerzza Mar 29 '18 at 10:07

1 Answers1

1

Your data is crashing RStudio for some reason, so lets simulate some:

precipitation <- sample(0:19, 180, replace = TRUE, prob = c(0.9, rep(0.1/19, 19)))/10
date <- round(Sys.time(), "min") + seq_len(180)*60
df <- data.frame(date, precipitation)

Now,because of the way your data is structured, if you want the number of hours that have some precipitation (df$precipitation > 0), you can just sum a logical vector and divide by 60:

sum(df$precipitation > 0) / 60

If you want it to be an object of class "difftime" just turn it into one, with units

x <- sum(df$precipitation > 0) / 60
class(x) <- "difftime"
attr(x, which = "units") <- "hours"

Now it will print as a difftime.

I've been trying to understand your desired behavior... I think it might be:

df$time_prec <- as.numeric(df$precipitation > 0)
head(df)
                 date precipitation time_prec
1 2018-03-28 11:13:00           0.0         0
2 2018-03-28 11:14:00           0.0         0
3 2018-03-28 11:15:00           0.0         0
4 2018-03-28 11:16:00           0.0         0
5 2018-03-28 11:17:00           0.9         1
6 2018-03-28 11:18:00           0.0         0

Your desired table (edit your post to include it, don't add it as an answer) shows the column to be numeric, and to have 0 for rows with no precipitation, and is blank for rows with some precipitation. You can't have a blank value for a numeric column (you can for character, but I wouldn't advise it), so here we made it 1 for rows with some precipitation.

De Novo
  • 7,120
  • 1
  • 23
  • 39
  • Thanks Dan for your answer, but I need to calculate for each hour the time when the rainfall is> 0 – cerzza Mar 28 '18 at 12:44
  • @cerzza is that what you're looking for? – De Novo Mar 28 '18 at 18:13
  • Hi Dan, what you kindly recommended me is a part of what I would like. The final result should be the sum of minutes in each for each hour. I'm trying to add an example of the final result, but not yet figured out how to do it – cerzza Mar 29 '18 at 12:23