首页 > 解决方案 > 如何将数据值舍入到 R 中的新列和组中

问题描述

我正在处理温度(开尔文)和事件数据,并创建了具有摄氏度转换的新列,我想将摄氏度值四舍五入到最接近的整数,并将 4 个数字分组。例如 29.15 摄氏度被四舍五入为 29 并分组在每 4 个数字的 bin(?) 中。这些组将从零开始并包含 4 个数字,例如 0-3、4-7、8-11、12-15 等。抱歉,我正在尝试使用更好的词,但我对 R 很陌生。如何我这样围成一团?下面是我到目前为止使用的代码和结果,但它不是我需要的舍入或分组。非常感谢!

tempDF <- data.frame(Kelvin = seq(240,320)) %>% #define an empty dat frame with temperatures going from 240 - 320 Kelvin
  mutate(Celsius = Kelvin - 273.15) %>%
  merge(New_AllTime_Temp, by.x = "Kelvin", by.y = "Temp", all.x = TRUE) %>% #Merge New_AllTime_Temp into the empty data frame, mapping each temperature to the data frame
  merge(New_Incident_Temp, by.x = "Kelvin", by.y = "temp", all.x = TRUE) %>% #Merge New_Incident_Temp into the empty datframe, keeping temperature mapping
  replace(is.na(.), 0) %>% ## Replace NA values with zeroes
  mutate(norm_counnt = scales::rescale(counnt, to=c(0,1))) %>%
  mutate(norm_incident = scales::rescale(incidents, to=c(0,1))) %>%
  mutate(diffs = norm_incident - norm_counnt) %>%
  mutate(rounded = round(Celsius, -2:4))```

"Kelvin" "Celsius" "counnt" "incidents" "norm_counnt" "norm_incident" "diffs" "rounded"
"1" 240 -33.15 0 0 0 0 0 0
"2" 241 -32.15 0 0 0 0 0 -30
"3" 242 -31.15 0 0 0 0 0 -31
"4" 243 -30.15 3 0 0.00146056475170399 0 -0.00146056475170399 -30.1
"5" 244 -29.15 9 0 0.00438169425511198 0 -0.00438169425511198 -29.15
"6" 245 -28.15 7 0 0.00340798442064265 0 -0.00340798442064265 -28.15
"7" 246 -27.15 11 1 0.0053554040895813 0.0196078431372549 0.0142524390476736 -27.15
"8" 247 -26.15 15 0 0.00730282375851996 0 -0.00730282375851996 0
"9" 248 -25.15 22 1 0.0107108081791626 0.0196078431372549 0.00889703495809229 -30
"10" 249 -24.15 11 1 0.0053554040895813 0.0196078431372549 0.0142524390476736 -24
"11" 250 -23.15 32 0 0.0155793573515093 0 -0.0155793573515093 -23.1
"12" 251 -22.15 33 0 0.0160662122687439 0 -0.0160662122687439 -22.15
"13" 252 -21.15 47 0 0.0228821811100292 0 -0.0228821811100292 -21.15
"14" 253 -20.15 107 1 0.0520934761441091 0.0196078431372549 -0.0324856330068542 -20.15
"15" 254 -19.15 117 0 0.0569620253164557 0 -0.0569620253164557 0
"16" 255 -18.15 162 2 0.0788704965920156 0.0392156862745098 -0.0396548103175058 -20
"17" 256 -17.15 221 4 0.107594936708861 0.0784313725490196 -0.0291635641598412 -17
"18" 257 -16.15 258 2 0.125608568646543 0.0392156862745098 -0.0863928823720335 -16.1
"19" 258 -15.15 272 3 0.132424537487829 0.0588235294117647 -0.0736010080760639 -15.15
"20" 259 -14.15 314 4 0.152872444011685 0.0784313725490196 -0.0744410714626649 -14.15
"21" 260 -13.15 409 4 0.199123661148978 0.0784313725490196 -0.120692288599958 -13.15
"22" 261 -12.15 478 11 0.232716650438169 0.215686274509804 -0.0170303759283655 0
"23" 262 -11.15 523 13 0.254625121713729 0.254901960784314 0.0002768390705844 -10
"24" 263 -10.15 574 8 0.279454722492697 0.156862745098039 -0.122591977394658 -10
"25" 264 -9.14999999999998 793 9 0.386075949367089 0.176470588235294 -0.209605361131794 -9.1
"26" 265 -8.14999999999998 924 14 0.44985394352483 0.274509803921569 -0.175344139603261 -8.15
"27" 266 -7.14999999999998 1108 18 0.539435248296008 0.352941176470588 -0.186494071825419 -7.15
"28" 267 -6.14999999999998 1082 17 0.526777020447907 0.333333333333333 -0.193443687114573 -6.15
"29" 268 -5.14999999999998 1198 15 0.583252190847128 0.294117647058824 -0.289134543788304 0
"30" 269 -4.14999999999998 1233 13 0.600292112950341 0.254901960784314 -0.345390152166027 0
"31" 270 -3.14999999999998 1244 17 0.605647517039922 0.333333333333333 -0.272314183706589 -3
"32" 271 -2.14999999999998 1496 32 0.728334956183057 0.627450980392157 -0.100883975790901 -2.1
"33" 272 -1.14999999999998 1565 25 0.761927945472249 0.490196078431373 -0.271731867040877 -1.15
"34" 273 -0.149999999999977 1870 35 0.910418695228822 0.686274509803922 -0.2241441854249 -0.15
"35" 274 0.850000000000023 2054 31 1 0.607843137254902 -0.392156862745098 0.85
"36" 275 1.85000000000002 2034 29 0.990262901655307 0.568627450980392 -0.421635450674915 0
"37" 276 2.85000000000002 1974 33 0.961051606621227 0.647058823529412 -0.313992783091815 0
"38" 277 3.85000000000002 1966 32 0.95715676728335 0.627450980392157 -0.329705786891193 4
"39" 278 4.85000000000002 2040 51 0.993184031158715 1 0.00681596884128532 4.9
"40" 279 5.85000000000002 1949 29 0.94888023369036 0.568627450980392 -0.380252782709968 5.85
"41" 280 6.85000000000002 2053 40 0.999513145082765 0.784313725490196 -0.215199419592569 6.85
"42" 281 7.85000000000002 1987 34 0.967380720545277 0.666666666666667 -0.300714053878611 7.85
"43" 282 8.85000000000002 1959 40 0.953748782862707 0.784313725490196 -0.169435057372511 0
"44" 283 9.85000000000002 1770 32 0.861733203505355 0.627450980392157 -0.234282223113199 10
"45" 284 10.85 1816 27 0.88412852969815 0.529411764705882 -0.354716764992268 11
"46" 285 11.85 1859 39 0.905063291139241 0.764705882352941 -0.140357408786299 11.9
"47" 286 12.85 2029 35 0.987828627069133 0.686274509803922 -0.301554117265212 12.85
"48" 287 13.85 1926 33 0.937682570593963 0.647058823529412 -0.290623747064551 13.85
"49" 288 14.85 1848 43 0.899707887049659 0.843137254901961 -0.0565706321476984 14.85
"50" 289 15.85 1823 33 0.887536514118793 0.647058823529412 -0.240477690589381 0
"51" 290 16.85 1662 24 0.809152872444012 0.470588235294118 -0.338564637149894 20
"52" 291 17.85 1578 31 0.7682570593963 0.607843137254902 -0.160413922141398 18
"53" 292 18.85 1425 12 0.693768257059396 0.235294117647059 -0.458474139412337 18.9
"54" 293 19.85 1318 17 0.641674780915287 0.333333333333333 -0.308341447581954 19.85
"55" 294 20.85 1204 19 0.586173320350535 0.372549019607843 -0.213624300742692 20.85
"56" 295 21.85 1029 18 0.500973709834469 0.352941176470588 -0.148032533363881 21.85
"57" 296 22.85 876 12 0.426484907497566 0.235294117647059 -0.191190789850507 0
"58" 297 23.85 735 13 0.357838364167478 0.254901960784314 -0.102936403383164 20
"59" 298 24.85 623 5 0.303310613437196 0.0980392156862745 -0.205271397750921 25
"60" 299 25.85 571 7 0.277994157740993 0.137254901960784 -0.140739255780209 25.9
"61" 300 26.85 512 5 0.249269717624148 0.0980392156862745 -0.151230501937874 26.85
"62" 301 27.85 417 5 0.203018500486855 0.0980392156862745 -0.10497928480058 27.85
"63" 302 28.85 345 14 0.167964946445959 0.274509803921569 0.10654485747561 28.85
"64" 303 29.85 294 6 0.143135345666991 0.117647058823529 -0.0254882868434618 0
"65" 304 30.85 253 3 0.12317429406037 0.0588235294117647 -0.0643507646486053 30
"66" 305 31.85 198 3 0.0963972736124635 0.0588235294117647 -0.0375737442006988 32
"67" 306 32.85 128 2 0.062317429406037 0.0392156862745098 -0.0231017431315272 32.9
"68" 307 33.85 88 2 0.0428432327166504 0.0392156862745098 -0.00362754644214063 33.85
"69" 308 34.85 64 1 0.0311587147030185 0.0196078431372549 -0.0115508715657636 34.85
"70" 309 35.85 48 0 0.0233690360272639 0 -0.0233690360272639 35.85
"71" 310 36.85 20 0 0.00973709834469328 0 -0.00973709834469328 0
"72" 311 37.85 16 0 0.00778967867575463 0 -0.00778967867575463 40
"73" 312 38.85 7 0 0.00340798442064265 0 -0.00340798442064265 39
"74" 313 39.85 1 0 0.000486854917234664 0 -0.000486854917234664 39.9
"75" 314 40.85 0 0 0 0 0 40.85
"76" 315 41.85 0 0 0 0 0 41.85
"77" 316 42.85 0 0 0 0 0 42.85
"78" 317 43.85 0 0 0 0 0 0
"79" 318 44.85 0 0 0 0 0 40
"80" 319 45.85 0 0 0 0 0 46
"81" 320 46.85 0 0 0 0 0 46.9

标签: rgroupingrounding

解决方案


舍入可以通过恰当命名的round函数来完成。该cut函数是针对连续数据而设计的,因此我们可以将实数的连续统切割为 -.5、3.5、7.5、11.5 ......

library(magrittr)

unrounded <- c(-12.6, -12.4, -.01, +.01, 12.4, 12.6)
rounded <- unrounded %>% round(digits = 0)


values <- c(1, 2, 4, 7, 10 ,20)
group <- values %>% cut(breaks = seq(-.5, 1000, 4))

推荐阅读