0

I'm trying to build a dataframe using NOAA weather data. I have the max and min temps by day and I'm trying to create a table which looks like this where:

  • row 1 are the dates (essentially transposed from my dataset)
  • row 2 are the daily high temps
  • row 3-85 are the temp of the cell above minus the interval (the interval is defined as the (hightemp - lowtemp)/84. 84 is the number of mapped rows.

I'm accomplishing rows 1 and 2 like this (max.F is a vector of the max temps):

rbind(as.character(data$Date),max.F)

What I'm struggling with are the rest of the rows. I was able to get row 3 by doing this, and then rbind to the rows above:

vector <- numeric(length=nrow(data))
for(i in seq_along(max.F)){vector[i] <- max.F[i] - interval[i]}

What I'm struggling with is how to create a function that will loop through and create the rest of the 82 rows. I'm still fairly new to R (and programming in general) and I've tried using the code snippet above within a function without much luck (it returns the same value as row 3 and only does one row). I imagine I have to tell it to repeat, but I'm not sure how to work that in:

mapping <- function(x){
  bin <- rbind()
  vector <- for(i in seq_along(max.F)){vector[i] <- max.F[i] - interval[i]}
  for(i in vector){
    vector[i] <- vector[i] - interval[i]
    bin <- rbind(bin, vector)}
  return(bin)}

Here's the dput (please let me know if I haven't done this properly, it's my first time using this function):

structure(list(Date = structure(c(17897, 17898, 17899, 17900, 17901, 17902, 17903, 17904, 17905, 17906, 17907, 17908, 17909, 17910, 17911, 17912, 17913, 17914, 17915, 17916, 17917, 17918, 17919, 17920, 17921, 17922, 17923, 17924, 17925, 17926, 17927, 17928, 17929, 17930, 17931, 17932, 17933, 17934, 17935, 17936, 17937, 17938, 17939, 17940, 17941, 17942, 17943, 17944, 17945, 17946, 17947, 17948, 17949, 17950, 17951, 17952, 17953, 17954, 17955, 17956, 17957, 17958, 17959, 17960, 17961, 17962, 17963, 17964, 17965, 17966, 17967, 17968, 17969, 17970, 17971, 17972, 17973, 17974, 17975, 17976, 17977, 17978, 17979, 17980, 17981, 17982, 17983, 17984, 17985, 17986, 17987, 17988, 17989, 17990, 17991, 17992, 17993, 17994, 17995, 17996, 17997, 17998, 17999, 18000, 18001, 18002, 18003, 18004, 18005, 18006, 18007, 18008, 18009, 18010, 18011, 18012, 18013, 18014, 18015, 18016, 18017, 18018, 18019, 18020, 18021, 18022, 18023, 18024, 18025, 18026, 18027, 18028, 18029, 18030, 18031, 18032, 18033, 18034, 18035, 18036, 18037, 18038, 18039, 18040, 18041, 18042, 18043, 18044, 18045, 18046, 18047, 18048, 18049, 18050, 18051, 18052, 18053, 18054, 18055, 18056, 18057, 18058, 18059, 18060, 18061, 18062, 18063, 18064, 18065, 18066, 18067, 18068, 18069, 18070, 18071, 18072, 18073, 18074, 18075, 18076, 18077, 18078, 18079, 18080, 18081, 18082, 18083, 18084, 18085, 18086, 18087, 18088, 18089, 18090, 18091, 18092, 18093, 18094, 18095, 18096, 18097, 18098, 18099, 18100, 18101, 18102, 18103), class = "Date"), Max = c(39, 30, 29, 40, 47, 52, 48, 53, 45, 21, 25, 29, 30, 32, 26, 33, 36, 34, 30, 30, 14, 17, 34, 31, 22, 5, 10, 28, 34, 5, -14, 6, 20, 41, 46, 49, 49, 35, 42, 13, 24, 28, 32, 32, 36, 45, 19, 29, 28, 29, 34, 35, 31, 43, 45, 33, 19, 25, 28, 26, 38, 32, 20, 13, 21, 31, 28, 41, 42, 38, 40, 48, 58, 64, 41, 40, 42, 44, 50, 45, 46, 50, 53, 49, 45, 51, 58, 65, 59, 45, 39, 49, 53, 58, 46, 53, 68, 68, 74, 66, 40, 59, 50, 53, 36, 51, 75, 76, 53, 55, 66, 77, 78, 63, 62, 69, 63, 44, 53, 51, 52, 59, 48, 58, 66, 73, 68, 54, 73, 62, 59, 50, 51, 64, 69, 73, 74, 56, 79, 73, 56, 54, 78, 72, 74, 81, 76, 73, 67, 75, 78, 81, 84, 73, 71, 82, 83, 81, 82, 79, 77, 74, 79, 70, 68, 79, 69, 66, 72, 80, 76, 72, 74, 80, 81, 77, 85, 89, 90, 90, 91, 90, 87, 90, 91, 89, 89, 85, 80, NA, 87, 93, 84, 87, 92, 91, 88, 83, 91, 87, 92, 94, 79, 77, 79, 81, 83), Min = c(30, 26, 23, 23, 29, 30, 32, 32, 18, 14, 14, 17, 27, 15, 23, 25, 28, 29, 26, 5, 2, 1, 17, 7, -7, -9, -11, -9, -3, -24, -25, -24, 6, 17, 39, 24, 24, 32, 3, 3, 3, 23, 28, 11, 11, 9, 8, 14, 21, 10, 10, 24, 20, 20, 33, 6, 6, 15, 12, 12, 24, 18, -4, -4, 7, 8, 17, 25, 31, 27, 23, 22, 37, 36, 28, 26, 27, 27, 27, 36, 35, 25, 25, 33, 26, 27, 31, 40, 37, 22, 22, 27, 34, 34, 41, 39, 39, 48, 45, 34, 34, 38, 33, 33, 26, 27, 47, 49, 42, 40, 40, 45, 49, 42, 43, 43, 42, 30, 30, 42, 40, 43, 40, 39, 41, 43, 45, 44, 47, 43, 40, 37, 39, 39, 39, 51, 55, 45, 42, 45, 45, 45, 54, 54, 54, 65, 53, 57, 53, 56, 59, 61, 54, 49, 53, 59, 59, 56, 59, 63, 60, 53, 57, 53, 48, 53, 57, 56, 55, 55, 56, 53, 56, 62, 65, 61, 63, 68, 70, 69, 69, 68, 69, 70, 70, 73, 73, 67, 62, NA, 63, 69, 61, 62, 69, 69, 70, 71, 74, 70, 77, 71, 65, 60, 61, 60, 62), Interval = c(0.107142857142857, 0.0476190476190476, 0.0714285714285714, 0.202380952380952, 0.214285714285714, 0.261904761904762, 0.19047619047619, 0.25, 0.321428571428571, 0.0833333333333333, 0.130952380952381, 0.142857142857143, 0.0357142857142857, 0.202380952380952, 0.0357142857142857, 0.0952380952380952, 0.0952380952380952, 0.0595238095238095, 0.0476190476190476, 0.297619047619048, 0.142857142857143, 0.19047619047619, 0.202380952380952, 0.285714285714286, 0.345238095238095, 0.166666666666667, 0.25, 0.44047619047619, 0.44047619047619, 0.345238095238095, 0.130952380952381, 0.357142857142857, 0.166666666666667, 0.285714285714286, 0.0833333333333333, 0.297619047619048, 0.297619047619048, 0.0357142857142857, 0.464285714285714, 0.119047619047619, 0.25, 0.0595238095238095, 0.0476190476190476, 0.25, 0.297619047619048, 0.428571428571429, 0.130952380952381, 0.178571428571429, 0.0833333333333333, 0.226190476190476, 0.285714285714286, 0.130952380952381, 0.130952380952381, 0.273809523809524, 0.142857142857143, 0.321428571428571, 0.154761904761905, 0.119047619047619, 0.19047619047619, 0.166666666666667, 0.166666666666667, 0.166666666666667, 0.285714285714286, 0.202380952380952, 0.166666666666667, 0.273809523809524, 0.130952380952381, 0.19047619047619, 0.130952380952381, 0.130952380952381, 0.202380952380952, 0.30952380952381, 0.25, 0.333333333333333, 0.154761904761905, 0.166666666666667, 0.178571428571429, 0.202380952380952, 0.273809523809524, 0.107142857142857, 0.130952380952381, 0.297619047619048, 0.333333333333333, 0.19047619047619, 0.226190476190476, 0.285714285714286, 0.321428571428571, 0.297619047619048, 0.261904761904762, 0.273809523809524, 0.202380952380952, 0.261904761904762, 0.226190476190476, 0.285714285714286, 0.0595238095238095, 0.166666666666667, 0.345238095238095, 0.238095238095238, 0.345238095238095, 0.380952380952381, 0.0714285714285714, 0.25, 0.202380952380952, 0.238095238095238, 0.119047619047619, 0.285714285714286, 0.333333333333333, 0.321428571428571, 0.130952380952381, 0.178571428571429, 0.30952380952381, 0.380952380952381, 0.345238095238095, 0.25, 0.226190476190476, 0.30952380952381, 0.25, 0.166666666666667, 0.273809523809524, 0.107142857142857, 0.142857142857143, 0.19047619047619, 0.0952380952380952, 0.226190476190476, 0.297619047619048, 0.357142857142857, 0.273809523809524, 0.119047619047619, 0.30952380952381, 0.226190476190476, 0.226190476190476, 0.154761904761905, 0.142857142857143, 0.297619047619048, 0.357142857142857, 0.261904761904762, 0.226190476190476, 0.130952380952381, 0.44047619047619, 0.333333333333333, 0.130952380952381, 0.107142857142857, 0.285714285714286, 0.214285714285714, 0.238095238095238, 0.19047619047619, 0.273809523809524, 0.19047619047619, 0.166666666666667, 0.226190476190476, 0.226190476190476, 0.238095238095238, 0.357142857142857, 0.285714285714286, 0.214285714285714, 0.273809523809524, 0.285714285714286, 0.297619047619048, 0.273809523809524, 0.19047619047619, 0.202380952380952, 0.25, 0.261904761904762, 0.202380952380952, 0.238095238095238, 0.30952380952381, 0.142857142857143, 0.119047619047619, 0.202380952380952, 0.297619047619048, 0.238095238095238, 0.226190476190476, 0.214285714285714, 0.214285714285714, 0.19047619047619, 0.19047619047619, 0.261904761904762, 0.25, 0.238095238095238, 0.25, 0.261904761904762, 0.261904761904762, 0.214285714285714, 0.238095238095238, 0.25, 0.19047619047619, 0.19047619047619, 0.214285714285714, 0.214285714285714, NA, 0.285714285714286, 0.285714285714286, 0.273809523809524, 0.297619047619048, 0.273809523809524, 0.261904761904762, 0.214285714285714, 0.142857142857143, 0.202380952380952, 0.202380952380952, 0.178571428571429, 0.273809523809524, 0.166666666666667, 0.202380952380952, 0.214285714285714, 0.25, 0.25)), class = "data.frame", row.names = c(NA, -207L))

Litmon
  • 247
  • 3
  • 18
  • Hello and welcome to stack overflow! please provide a minimally reproducible example by using `dput(data)`. If your data is too big, use `dput(head(data))` – boski Jul 30 '19 at 12:39
  • "row 3-85 are the temp of the cell above minus the interval (the interval is defined as the (hightemp - lowtemp)/84. 84 is the number of mapped rows." ... you mean hightemp and lowtemp of the row immediately above? – Amit Jul 30 '19 at 12:57
  • Thanks for responding! I'm happy to do that, but please forgive my ignorance, I'm not sure how to use dput(data)...do I put that into R? How do I get it to stack overflow? – Litmon Jul 30 '19 at 12:59
  • @Amit, the first row of temps is the max temp. The second row is the maxtemp - interval. The third row is maxtemp - 2*interval, etc. The last row (row 84) is the same as the mintemp. – Litmon Jul 30 '19 at 13:01
  • @boski, I added the structure from the dput function, following what they said in this thread: https://stackoverflow.com/questions/49994249/example-of-using-dput. Please let me know if there's anything else I need to do. – Litmon Jul 30 '19 at 13:12
  • @Amit alternatively, you could think of the third row as row2-interval. Whichever is easier to calculate for the function. – Litmon Jul 30 '19 at 13:24

1 Answers1

0

Here is my solution. There is one caveat. Since your Date is formatted as integer in the DF that you pasted, to convert it back to Date, you need an origin. I have choosen "1970-01-01" which gives me the starting date as "2019-01-01". If this is low or high, accordingly adjust the origin to match it. Rest of the code is as below.

Date  <- as.Date(c(17897, 17898, 17899, 17900, 17901, 17902, 17903, 17904, 17905, 17906, 17907, 17908, 17909, 17910, 17911, 17912, 17913, 17914, 17915, 17916, 17917, 17918, 17919, 17920, 17921, 17922, 17923, 17924, 17925, 17926, 17927, 17928, 17929, 17930, 17931, 17932, 17933, 17934, 17935, 17936, 17937, 17938, 17939, 17940, 17941, 17942, 17943, 17944, 17945, 17946, 17947, 17948, 17949, 17950, 17951, 17952, 17953, 17954, 17955, 17956, 17957, 17958, 17959, 17960, 17961, 17962, 17963, 17964, 17965, 17966, 17967, 17968, 17969, 17970, 17971, 17972, 17973, 17974, 17975, 17976, 17977, 17978, 17979, 17980, 17981, 17982, 17983, 17984, 17985, 17986, 17987, 17988, 17989, 17990, 17991, 17992, 17993, 17994, 17995, 17996, 17997, 17998, 17999, 18000, 18001, 18002, 18003, 18004, 18005, 18006, 18007, 18008, 18009, 18010, 18011, 18012, 18013, 18014, 18015, 18016, 18017, 18018, 18019, 18020, 18021, 18022, 18023, 18024, 18025, 18026, 18027, 18028, 18029, 18030, 18031, 18032, 18033, 18034, 18035, 18036, 18037, 18038, 18039, 18040, 18041, 18042, 18043, 18044, 18045, 18046, 18047, 18048, 18049, 18050, 18051, 18052, 18053, 18054, 18055, 18056, 18057, 18058, 18059, 18060, 18061, 18062, 18063, 18064, 18065, 18066, 18067, 18068, 18069, 18070, 18071, 18072, 18073, 18074, 18075, 18076, 18077, 18078, 18079, 18080, 18081, 18082, 18083, 18084, 18085, 18086, 18087, 18088, 18089, 18090, 18091, 18092, 18093, 18094, 18095, 18096, 18097, 18098, 18099, 18100, 18101, 18102, 18103),format="%Y-%m-%d",origin="1970-01-01")
Max <- c(39, 30, 29, 40, 47, 52, 48, 53, 45, 21, 25, 29, 30, 32, 26, 33, 36, 34, 30, 30, 14, 17, 34, 31, 22, 5, 10, 28, 34, 5, -14, 6, 20, 41, 46, 49, 49, 35, 42, 13, 24, 28, 32, 32, 36, 45, 19, 29, 28, 29, 34, 35, 31, 43, 45, 33, 19, 25, 28, 26, 38, 32, 20, 13, 21, 31, 28, 41, 42, 38, 40, 48, 58, 64, 41, 40, 42, 44, 50, 45, 46, 50, 53, 49, 45, 51, 58, 65, 59, 45, 39, 49, 53, 58, 46, 53, 68, 68, 74, 66, 40, 59, 50, 53, 36, 51, 75, 76, 53, 55, 66, 77, 78, 63, 62, 69, 63, 44, 53, 51, 52, 59, 48, 58, 66, 73, 68, 54, 73, 62, 59, 50, 51, 64, 69, 73, 74, 56, 79, 73, 56, 54, 78, 72, 74, 81, 76, 73, 67, 75, 78, 81, 84, 73, 71, 82, 83, 81, 82, 79, 77, 74, 79, 70, 68, 79, 69, 66, 72, 80, 76, 72, 74, 80, 81, 77, 85, 89, 90, 90, 91, 90, 87, 90, 91, 89, 89, 85, 80, NA, 87, 93, 84, 87, 92, 91, 88, 83, 91, 87, 92, 94, 79, 77, 79, 81, 83)
Min <- c(30, 26, 23, 23, 29, 30, 32, 32, 18, 14, 14, 17, 27, 15, 23, 25, 28, 29, 26, 5, 2, 1, 17, 7, -7, -9, -11, -9, -3, -24, -25, -24, 6, 17, 39, 24, 24, 32, 3, 3, 3, 23, 28, 11, 11, 9, 8, 14, 21, 10, 10, 24, 20, 20, 33, 6, 6, 15, 12, 12, 24, 18, -4, -4, 7, 8, 17, 25, 31, 27, 23, 22, 37, 36, 28, 26, 27, 27, 27, 36, 35, 25, 25, 33, 26, 27, 31, 40, 37, 22, 22, 27, 34, 34, 41, 39, 39, 48, 45, 34, 34, 38, 33, 33, 26, 27, 47, 49, 42, 40, 40, 45, 49, 42, 43, 43, 42, 30, 30, 42, 40, 43, 40, 39, 41, 43, 45, 44, 47, 43, 40, 37, 39, 39, 39, 51, 55, 45, 42, 45, 45, 45, 54, 54, 54, 65, 53, 57, 53, 56, 59, 61, 54, 49, 53, 59, 59, 56, 59, 63, 60, 53, 57, 53, 48, 53, 57, 56, 55, 55, 56, 53, 56, 62, 65, 61, 63, 68, 70, 69, 69, 68, 69, 70, 70, 73, 73, 67, 62, NA, 63, 69, 61, 62, 69, 69, 70, 71, 74, 70, 77, 71, 65, 60, 61, 60, 62)
Interval <- c(0.107142857142857, 0.0476190476190476, 0.0714285714285714, 0.202380952380952, 0.214285714285714, 0.261904761904762, 0.19047619047619, 0.25, 0.321428571428571, 0.0833333333333333, 0.130952380952381, 0.142857142857143, 0.0357142857142857, 0.202380952380952, 0.0357142857142857, 0.0952380952380952, 0.0952380952380952, 0.0595238095238095, 0.0476190476190476, 0.297619047619048, 0.142857142857143, 0.19047619047619, 0.202380952380952, 0.285714285714286, 0.345238095238095, 0.166666666666667, 0.25, 0.44047619047619, 0.44047619047619, 0.345238095238095, 0.130952380952381, 0.357142857142857, 0.166666666666667, 0.285714285714286, 0.0833333333333333, 0.297619047619048, 0.297619047619048, 0.0357142857142857, 0.464285714285714, 0.119047619047619, 0.25, 0.0595238095238095, 0.0476190476190476, 0.25, 0.297619047619048, 0.428571428571429, 0.130952380952381, 0.178571428571429, 0.0833333333333333, 0.226190476190476, 0.285714285714286, 0.130952380952381, 0.130952380952381, 0.273809523809524, 0.142857142857143, 0.321428571428571, 0.154761904761905, 0.119047619047619, 0.19047619047619, 0.166666666666667, 0.166666666666667, 0.166666666666667, 0.285714285714286, 0.202380952380952, 0.166666666666667, 0.273809523809524, 0.130952380952381, 0.19047619047619, 0.130952380952381, 0.130952380952381, 0.202380952380952, 0.30952380952381, 0.25, 0.333333333333333, 0.154761904761905, 0.166666666666667, 0.178571428571429, 0.202380952380952, 0.273809523809524, 0.107142857142857, 0.130952380952381, 0.297619047619048, 0.333333333333333, 0.19047619047619, 0.226190476190476, 0.285714285714286, 0.321428571428571, 0.297619047619048, 0.261904761904762, 0.273809523809524, 0.202380952380952, 0.261904761904762, 0.226190476190476, 0.285714285714286, 0.0595238095238095, 0.166666666666667, 0.345238095238095, 0.238095238095238, 0.345238095238095, 0.380952380952381, 0.0714285714285714, 0.25, 0.202380952380952, 0.238095238095238, 0.119047619047619, 0.285714285714286, 0.333333333333333, 0.321428571428571, 0.130952380952381, 0.178571428571429, 0.30952380952381, 0.380952380952381, 0.345238095238095, 0.25, 0.226190476190476, 0.30952380952381, 0.25, 0.166666666666667, 0.273809523809524, 0.107142857142857, 0.142857142857143, 0.19047619047619, 0.0952380952380952, 0.226190476190476, 0.297619047619048, 0.357142857142857, 0.273809523809524, 0.119047619047619, 0.30952380952381, 0.226190476190476, 0.226190476190476, 0.154761904761905, 0.142857142857143, 0.297619047619048, 0.357142857142857, 0.261904761904762, 0.226190476190476, 0.130952380952381, 0.44047619047619, 0.333333333333333, 0.130952380952381, 0.107142857142857, 0.285714285714286, 0.214285714285714, 0.238095238095238, 0.19047619047619, 0.273809523809524, 0.19047619047619, 0.166666666666667, 0.226190476190476, 0.226190476190476, 0.238095238095238, 0.357142857142857, 0.285714285714286, 0.214285714285714, 0.273809523809524, 0.285714285714286, 0.297619047619048, 0.273809523809524, 0.19047619047619, 0.202380952380952, 0.25, 0.261904761904762, 0.202380952380952, 0.238095238095238, 0.30952380952381, 0.142857142857143, 0.119047619047619, 0.202380952380952, 0.297619047619048, 0.238095238095238, 0.226190476190476, 0.214285714285714, 0.214285714285714, 0.19047619047619, 0.19047619047619, 0.261904761904762, 0.25, 0.238095238095238, 0.25, 0.261904761904762, 0.261904761904762, 0.214285714285714, 0.238095238095238, 0.25, 0.19047619047619, 0.19047619047619, 0.214285714285714, 0.214285714285714, NA, 0.285714285714286, 0.285714285714286, 0.273809523809524, 0.297619047619048, 0.273809523809524, 0.261904761904762, 0.214285714285714, 0.142857142857143, 0.202380952380952, 0.202380952380952, 0.178571428571429, 0.273809523809524, 0.166666666666667, 0.202380952380952, 0.214285714285714, 0.25, 0.25)
My_DF <- NULL
My_DF <- data.frame()
My_DF <- data.frame(rbind(My_DF,Max))
for(i in c(1:83))
{
  Computed_Vec <- Max - (i-0)*Interval
  My_DF <- rbind(My_DF,Computed_Vec)
}
My_DF <- rbind(My_DF,Min)
Column_Names <- c()
for (i in length(Date)){Column_Names <- c(Column_Names, paste("Column_",i))}
colnames(My_DF) <- Date
Row_Names <- c("Max")
for (i in c(1:83)){Row_Names <- c(Row_Names, paste("Max - Interval*",(i)))}
Row_Names <- c(Row_Names,"Min")
rownames(My_DF) <- Row_Names
View(My_DF)

This gives me the following output. Snippet attached in the image. I am not sure if this is what you wanted. Please let me know.

Output as I see

Amit
  • 2,018
  • 1
  • 8
  • 12
  • Thank you! I'm running it now, it's just taking quite a while to process (I'm importing it into googlesheets to check how it comes through, and that's what's taking ages). Could I ask what the (i-0)*interval is doing? isn't i-0 then same thing as i? What's the difference between that and i*interval? @Amit – Litmon Jul 30 '19 at 14:58
  • @Litmon All I can do is laugh at myself. Yes it is essentially one and same thing. I was talking on phone and typing and there you see ... – Amit Jul 30 '19 at 15:01