José Ricardo
José Ricardo

Reputation: 301

Import time series from .txt

How can I import a dataset from a txt to R with a time series like mm:ss.ms? Here's an example:

The first column is the time series of multiple measures of luminescence, all the other columns are the luminescence values

00:00.138   1143.85 1332.34 1284.34 1541.83 1072.5  1246.18 1210.49 976.64  1410.89 1422.71 1455.1  1385.26 958.78  1168.19 1020.04 1028.59 1198.61 1152.17 1322.07 1270.1  1433.54 856.42  1182.04 1264.99 1160.18 1254.08 1167.76 1280.5  928.55  1303.5  1321.49 \n
00:02.128   1120.18 1293.45 1255.17 1500.33 1035.3  1219.7  1182.03 938.74  1369.64 1375.9  1418.06 1347.51 923.98  1140.92 984.36  996.8   1161.72 1123.83 1281.82 1240.04 1400.63 829.52  1139.81 1223.5  1134.38 1226.37 1145.82 1249.65 890.34  1279.68 1291.73 \n
00:04.129   1127.1  1309.34 1252.12 1522.06 1045.65 1223.24 1185.66 944.61  1377.33 1392.4  1416.99 1345.23 929.08  1144.36 995.22  998.6   1174.05 1129.54 1281.82 1246.95 1405.14 840.18  1150.95 1235.29 1139.11 1222.44 1153.29 1257.81 904.89  1288.29 1305.09 \n
00:06.129   1135.38 1325.54 1273.34 1530.9  1055.82 1222.98 1184.99 955.38  1397.07 1403.03 1430.81 1363.3  940.23  1161.62 1003.57 1016.42 1185.46 1147.48 1312.82 1266.7  1415.09 840.5   1157.53 1249.42 1145.2  1239.23 1142.57 1257.44 910.87  1288.15 1316.47 \n
00:08.129   1131.93 1321.07 1261.69 1526.63 1053.65 1233.64 1202.59 959.01  1400.66 1421.88 1453.22 1370.15 946.34  1163.81 1005.93 1017.11 1193.76 1141.68 1312.78 1259.71 1417.27 842.04  1169.47 1248.45 1128.15 1234.49 1170.91 1277.85 909.26  1291.65 1303.67

I'm using the following code to import the data:

db<-read.table("reading_1.txt",sep="\t",skipNul = TRUE,skip=6,dec=".")

The problem is that R is handling the first column as factor and I need to set it as time.

str(db)
 'data.frame':  5 obs. of  33 variables:
 $ V1 : Factor w/ 5 levels "00:00.092","00:02.069",..: 1 2 3 4 5
 $ V2 : num  1444 1457 1443 1443 1437
 $ V3 : num  1367 1373 1362 1361 1362
 $ V4 : num  1381 1390 1374 1380 1377
 $ V5 : num  1417 1418 1410 1406 1403
 ...

Thank you very much.

Upvotes: 1

Views: 220

Answers (1)

pogibas
pogibas

Reputation: 28379

Does this work:

library(data.table)
library(lubridate)

db <- fread("reading_1.txt")
db$V1 <- ms(db$V1)

class(db$V1)
[1] "Period"
attr(,"package")
[1] "lubridate"

Use ms from lubridate to transform character into a period.

Upvotes: 2

Related Questions