在 R 中生成多行向量的函数

Function to generate multiple rows of vectors in R

我正在尝试使用 NOAA 天气数据构建数据框。我每天都有最高和最低温度,我正在尝试创建一个 table,它看起来像这样,其中:

我正在完成第 1 行和第 2 行(max.F 是最大温度的向量):

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

我正在努力解决的是其余的行。通过这样做,我能够获得第 3 行,然后 rbind 到上面的行:

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

我正在苦苦挣扎的是如何创建一个循环遍历并创建其余 82 行的函数。我对 R(和一般编程)还是很陌生,我尝试在一个函数中使用上面的代码片段,但运气不佳(它 returns 与第 3 行的值相同,并且只执行一行)。我想我必须告诉它重复,但我不确定如何工作:

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)}

这是输入(如果我没有正确完成,请告诉我,这是我第一次使用这个功能):

结构(列表(日期=结构(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"), 最大值 = 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), 最小 = 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), 区间 = 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, 不适用, 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))

这是我的解决方案。有一个警告。由于您的日期在您粘贴的 DF 中被格式化为整数,因此要将其转换回日期,您需要一个原点。我选择了“1970-01-01”,这让我的开始日期为“2019-01-01”。如果这是低或高,相应地调整原点以匹配它。其余代码如下。

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)

这给了我以下输出。图片中附有片段。我不确定这是否是您想要的。请告诉我。