大家好
我有一列从0-2355的数字,我希望把它们转换成0:00, 23:55这样的时间格式
尝试了 times <- format(otter$time, format = "%h-%m")
或是运算otter$time <- as.numeric(substr(otter[,3],1,2))*60 + as.numeric(substr(otter[3,],3,4))
都没有成功,请问有什么方法可以实现转换?
谢谢。
大家好
我有一列从0-2355的数字,我希望把它们转换成0:00, 23:55这样的时间格式
尝试了 times <- format(otter$time, format = "%h-%m")
或是运算otter$time <- as.numeric(substr(otter[,3],1,2))*60 + as.numeric(substr(otter[3,],3,4))
都没有成功,请问有什么方法可以实现转换?
谢谢。
您好,我在使用reprex的时候出现了问题
otter$time
#> Error in eval(expr, envir, enclos): 找不到对象'otter'
format(strptime(otter$time, format = "%H%M"), format = "%H:%M")
#> Error in strptime(otter$time, format = "%H%M"): 找不到对象'otter'
<sup>Created on 2018-10-23 by the reprex package (v0.2.1)</sup>
但是我运行您的format 代码 得到的结果中存在NA,是因为所有四位数一下的数字都没有转换
抱歉我直接贴上运行结果
otter$time
[1] 840 850 900 910 920 930 940 950 1000 1010 1020 1030 1040 1050 1100 1110 1120 1130 1140 1150 1200
[22] 1210 1220 1230 1240 1250 1300 1310 1320 1330 1340 1350 1400 1410 1420 1430 1440 1450 1500 1510 1520 1530
[43] 1540 1550 1600 1610 1620 1630 1640 1650 1700 1710 1720 1730 1740 1750 1800 1810 1820 1830 1840 1850 1900
[64] 1910 1920 1930 1940 1950 2000 2010 2020 2030 2040 2050 2100 2110 2120 2130 2140 2150 2200 2210 2230 2240
[85] 2250 2300 2310 2320 2330 2340 2350 0 10 20 30 40 50 100 110 120 130 140 150 200 210
[106] 220 230 240 250 300 310 320 330 340 930 940 950 1000 1010 1020 1030 1040 1050 1100 1110 1120
[127] 1130 1140 1150 1200 1210 1220 1230 1240 1250 1300 1310 1320 1330 1340 1350 1400 1410 1420 1430 1440 1450
[148] 1500 1510 1520 1530 1540 1550 1600 1610 1620 1630 1640 1650 1700 1710 1720 1730 1740 1750 1800 1810 1820
[169] 1830 1840 1850 1900 1910 1920 1930 1940 1950 2000 2010 2020 2030 2040 2050 2100 2110 2120 2130 2140 2150
[190] 2200 2210 2220 2230 2240 2250 2300 2310 2320 2330 2340 2350 0 10 20 30 40 50 100 110 120
[211] 130 140 150 200
format(strptime(otter$time, format = "%H%M"), format = "%H:%M")
[1] NA NA NA NA NA NA NA NA "10:00" "10:10" "10:20" "10:30" "10:40"
[14] "10:50" "11:00" "11:10" "11:20" "11:30" "11:40" "11:50" "12:00" "12:10" "12:20" "12:30" "12:40" "12:50"
[27] "13:00" "13:10" "13:20" "13:30" "13:40" "13:50" "14:00" "14:10" "14:20" "14:30" "14:40" "14:50" "15:00"
[40] "15:10" "15:20" "15:30" "15:40" "15:50" "16:00" "16:10" "16:20" "16:30" "16:40" "16:50" "17:00" "17:10"
[53] "17:20" "17:30" "17:40" "17:50" "18:00" "18:10" "18:20" "18:30" "18:40" "18:50" "19:00" "19:10" "19:20"
[66] "19:30" "19:40" "19:50" "20:00" "20:10" "20:20" "20:30" "20:40" "20:50" "21:00" "21:10" "21:20" "21:30"
[79] "21:40" "21:50" "22:00" "22:10" "22:30" "22:40" "22:50" "23:00" "23:10" "23:20" "23:30" "23:40" "23:50"
[92] NA NA NA NA NA NA "10:00" "11:00" "12:00" "13:00" "14:00" "15:00" "20:00"
[105] "21:00" "22:00" "23:00" "00:00" NA NA NA NA NA NA NA NA NA
[118] "10:00" "10:10" "10:20" "10:30" "10:40" "10:50" "11:00" "11:10" "11:20" "11:30" "11:40" "11:50" "12:00"
[131] "12:10" "12:20" "12:30" "12:40" "12:50" "13:00" "13:10" "13:20" "13:30" "13:40" "13:50" "14:00" "14:10"
[144] "14:20" "14:30" "14:40" "14:50" "15:00" "15:10" "15:20" "15:30" "15:40" "15:50" "16:00" "16:10" "16:20"
[157] "16:30" "16:40" "16:50" "17:00" "17:10" "17:20" "17:30" "17:40" "17:50" "18:00" "18:10" "18:20" "18:30"
[170] "18:40" "18:50" "19:00" "19:10" "19:20" "19:30" "19:40" "19:50" "20:00" "20:10" "20:20" "20:30" "20:40"
[183] "20:50" "21:00" "21:10" "21:20" "21:30" "21:40" "21:50" "22:00" "22:10" "22:20" "22:30" "22:40" "22:50"
[196] "23:00" "23:10" "23:20" "23:30" "23:40" "23:50" NA NA NA NA NA NA "10:00"
[209] "11:00" "12:00" "13:00" "14:00" "15:00" "20:00"
下次贴数据,建议试试dput()
,比如 dput(mtcars)
:
structure(list(mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1, 14.3,
24.4, 22.8, 19.2, 17.8, 16.4, 17.3, 15.2, 10.4, 10.4, 14.7, 32.4,
30.4, 33.9, 21.5, 15.5, 15.2, 13.3, 19.2, 27.3, 26, 30.4, 15.8,
19.7, 15, 21.4), cyl = c(6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8,
8, 8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 4, 8, 6, 8, 4),
disp = c(160, 160, 108, 258, 360, 225, 360, 146.7, 140.8,
167.6, 167.6, 275.8, 275.8, 275.8, 472, 460, 440, 78.7, 75.7,
71.1, 120.1, 318, 304, 350, 400, 79, 120.3, 95.1, 351, 145,
301, 121), hp = c(110, 110, 93, 110, 175, 105, 245, 62, 95,
123, 123, 180, 180, 180, 205, 215, 230, 66, 52, 65, 97, 150,
150, 245, 175, 66, 91, 113, 264, 175, 335, 109), drat = c(3.9,
3.9, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,
3.07, 3.07, 3.07, 2.93, 3, 3.23, 4.08, 4.93, 4.22, 3.7, 2.76,
3.15, 3.73, 3.08, 4.08, 4.43, 3.77, 4.22, 3.62, 3.54, 4.11
), wt = c(2.62, 2.875, 2.32, 3.215, 3.44, 3.46, 3.57, 3.19,
3.15, 3.44, 3.44, 4.07, 3.73, 3.78, 5.25, 5.424, 5.345, 2.2,
1.615, 1.835, 2.465, 3.52, 3.435, 3.84, 3.845, 1.935, 2.14,
1.513, 3.17, 2.77, 3.57, 2.78), qsec = c(16.46, 17.02, 18.61,
19.44, 17.02, 20.22, 15.84, 20, 22.9, 18.3, 18.9, 17.4, 17.6,
18, 17.98, 17.82, 17.42, 19.47, 18.52, 19.9, 20.01, 16.87,
17.3, 15.41, 17.05, 18.9, 16.7, 16.9, 14.5, 15.5, 14.6, 18.6
), vs = c(0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1), am = c(1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1), gear = c(4, 4, 4, 3,
3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3, 3,
3, 3, 4, 5, 5, 5, 5, 5, 4), carb = c(4, 4, 1, 1, 2, 1, 4,
2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2, 2, 4, 2, 1,
2, 2, 4, 6, 8, 2)), row.names = c("Mazda RX4", "Mazda RX4 Wag",
"Datsun 710", "Hornet 4 Drive", "Hornet Sportabout", "Valiant",
"Duster 360", "Merc 240D", "Merc 230", "Merc 280", "Merc 280C",
"Merc 450SE", "Merc 450SL", "Merc 450SLC", "Cadillac Fleetwood",
"Lincoln Continental", "Chrysler Imperial", "Fiat 128", "Honda Civic",
"Toyota Corolla", "Toyota Corona", "Dodge Challenger", "AMC Javelin",
"Camaro Z28", "Pontiac Firebird", "Fiat X1-9", "Porsche 914-2",
"Lotus Europa", "Ford Pantera L", "Ferrari Dino", "Maserati Bora",
"Volvo 142E"), class = "data.frame")
这样别人直接复制上面的代码到R里就可以得到跟你一样的样本数据了。