R: Convert time series xyz to raster - r

I have a time series dt (time, lat, value) composed of 204 rows and 19 columns.
I would like to plot my time series as raster plot using the raster and oceanmap packages.
I am trying to convert the data frame to raster but with some problems
Here my code:
library(raster)
dt <- t(data.table::data.table(tb)) #tb is the previous matrix with values
rownames(dt) <- tm.mtx #my time as Dates
colnames(dt) <- cdt #lat coordinates
dt[is.nan(dt)] <- NA
dt.melt <- reshape2::melt(dt, id=c(row.names(dt)))
spg <- dt.melt
colnames(spg) <- c("x","y","z")
coordinates(spg) <- ~ x + y
it returns this:
Error in .local(obj, ...) :
cannot derive coordinates from non-numeric matrix
I understood I have to convert Dates as numeric, so:
rownames(dt) <- tm #vector of time in seconds from "2002-08-15"
dt.melt <- reshape2::melt(dt, id=c(row.names(dt)))
spg <- dt.melt
colnames(spg) <- c("x","y","z")
coordinates(spg) <- ~ x + y
gridded(spg) = T
returning:
suggested tolerance minimum: 0.107308
Error in points2grid(points, tolerance, round) :
dimension 1 : coordinate intervals are not constant
from here I do not know what to do.
I attach below some partial data
head(dt)
-46.4375070233063 -46.4791737000479 -46.5208403767895 -46.5625070535311
2002-08-15 0.1736002 0.1750216 0.1732153 0.1774634
2002-09-15 0.1544965 0.1651486 0.1691601 0.1751704
2002-10-15 0.1882919 0.1835454 0.1881429 0.1941372
2002-11-15 0.2535837 0.2623392 0.2630876 0.2709922
2002-12-15 0.2187145 0.2208886 0.2157844 0.2170794
2003-01-15 0.1413760 0.1334794 0.1383164 0.1370846
-46.6041737302728 -46.6458404070144 -46.687507083756 -46.7291737604976
2002-08-15 0.1773711 0.1833218 0.1922676 0.1941034
2002-09-15 0.1771452 0.1796460 0.1761335 0.1702302
2002-10-15 0.1968857 0.2063893 0.2040608 0.1979190
2002-11-15 0.2707081 0.2532419 0.2730597 0.2807892
2002-12-15 0.2154748 0.2144032 0.2160844 0.2267726
2003-01-15 0.1385788 0.1351013 0.1375451 0.1544153
-46.7708404372393 -46.8125071139809 -46.8541737907225 -46.8958404674641
2002-08-15 0.1936037 0.1870466 0.1829850 0.1970123
2002-09-15 0.1650944 0.1764924 0.1801598 0.1772189
2002-10-15 0.1889605 0.1884651 0.1911619 0.1967313
2002-11-15 0.2862864 0.3205168 0.3466025 0.3267133
2002-12-15 0.2262225 0.2184253 0.2562738 0.2705393
2003-01-15 0.2592504 0.3460798 0.4375293 0.5184022
-46.9375071442058 -46.9791738209474 -47.020840497689 -47.0625071744306
2002-08-15 0.2062590 0.2063133 0.2125093 0.2129866
2002-09-15 0.1775195 0.1759667 0.1642771 0.1685364
2002-10-15 0.2002673 0.1997982 0.1997716 0.1917371
2002-11-15 0.2311774 0.3478559 0.3199840 0.3296006
2002-12-15 0.3308617 0.3285339 0.5459298 0.6657989
2003-01-15 0.6338662 0.6308048 0.5857707 0.4804939
-47.1041738511723 -47.1458405279139 -47.1875072046555
2002-08-15 0.2157603 0.2188090 0.2243740
2002-09-15 0.1742391 0.1754706 0.1729936
2002-10-15 0.2043249 0.2011236 0.2345921
2002-11-15 0.3316522 0.3430276 0.3336774
2002-12-15 0.6513002 0.5746747 0.4276562
2003-01-15 0.4010864 0.4328661 0.4627818
tm
[1] 1 2678401 5270401 7948801 10540801 13219201 15897601 18316801
[9] 20995201 23587201 26265601 28857601 31536001 34214401 36806401 39484801
[17] 42076801 44755201 47433601 49939201 52617601 55209601 57888001 60480001
[25] 63158401 65836801 68428801 71107201 73699201 76377601 79056001 81475201
[33] 84153601 86745601 89424001 92016001 94694401 97372801 99964801 102643201
[41] 105235201 107913601 110592001 113011201 115689601 118281601 120960001 123552001
[49] 126230401 128908801 131500801 134179201 136771201 139449601 142128001 144547201
[57] 147225601 149817601 152496001 155088001 157766401 160444801 163036801 165715201
[65] 168307201 170985601 173664001 176169601 178848001 181440001 184118401 186710401
[73] 189388801 192067201 194659201 197337601 199929601 202608001 205286401 207705601
[81] 210384001 212976000 215654401 218246401 220924801 223603201 226195201 228873601
[89] 231465601 234144001 236822401 239241601 241920001 244512001 247190401 249782401
[97] 252460801 255139201 257731201 260409601 263001601 265680001 268358401 270777601
[105] 273456001 276048001 278726401 281318401 283996801 286675201 289267201 291945601
[113] 294537601 297216001 299894401 302400001 305078401 307670401 310348801 312940801
[121] 315619201 318297601 320889601 323568001 326160001 328838401 331516801 333936001
[129] 336614401 339206401 341884801 344476801 347155201 349833601 352425601 355104001
[137] 357696001 360374401 363052801 365472001 368150401 370742401 373420801 376012801
[145] 378691201 381369601 383961601 386640001 389232001 391910401 394588801 397008001
[153] 399686401 402278401 404956801 407548801 410227201 412905601 415497601 418176001
[161] 420768001 423446400 426124800 428630400 431308800 433900800 436579200 439171200
[169] 441849600 444528000 447120000 449798400 452390400 455068400 457747200 460166400
[177] 462844800 465436800 468115200 470707200 473385600 476064000 478656000 481334400
[185] 483926400 486604800 489283200 491702400 494380800 496972800 499651200 502243200
[193] 504921600 507600000 510192000 512870400 515462400 518140800 520819200 523238400
[201] 525916800 528508800 531187200 533779200
data
http://www.filedropper.com/df

Related

generate sequence of numbers with 000000 as start value

I am writing a function that takes in a start and end day in the format of dhhmmss (day-hour-minutes-second) and calculates the length of the Palindrome numbers between the start and end dhhmmss.
By defintion the start hhmmss is 000000 and end hhmmss is 235959.
My function has to take only the start d and end d and calculate the length of the Palindrome numbers between these two
Here's how I did it
Reverse.numberAsString <- function(x){ # Reverse using string manipulation
x.out <- as.character(x) # convert number to a character string
x.out <- unlist(strsplit(x.out, '')) # break the string up into a vector
x.out <- rev(x.out) # reverse it
x.out <- paste(x.out, collapse='') # join it back together
x.out <- as.numeric(x.out) # turn it back to a number
return(x.out)
}
is.Palindrome <- function(x){
x == sapply(x,Reverse.numberAsString)
}
palindrom_fun <- function(n1, n2){
if (n1 > n2) { print('n1 cannot be > n2')
} else {
n1.mod <- as.numeric(paste(c(n1, "000000"), collapse = ""))
n2.mod <- as.numeric(paste(c(n2, "235959"), collapse = ""))
x <- seq(from = n1.mod, to = n2.mod, by = 1)
palindrome_number <- x[is.Palindrome(x)]
length.palindrom <- length(palindrome_number)
return(length.palindrom)
}
}
palindrom_fun(1, 2)
# 1236
However, the above function will not work if n1 = 0 and n1 = 1 because of the line
n1.mod <- as.numeric(paste(c(n1, "000000"), collapse = ""))
n2.mod <- as.numeric(paste(c(n2, "235959"), collapse = ""))
since R is not able to create a sequence of number from 0000000 to 1235959. How can I get my function to work for this case?
You may compare head and reversed tail of character vectors using : (since head and tail are slow). For the desired sequence you may use sprintf to generate leading zeros.
isPalindrome <- Vectorize(function(x) {
s <- el(strsplit(as.character(x), ""))
ll <- length(s)
l2 <- pmax(floor(ll / 2), 1)
# out <- all(head(s, l) == rev(tail(s, l))) ## slower
out <- all(s[1:l2] == s[ll:(ll - l2 + 1)])
return(out)
})
## Test
x <- c("0000000", "1123456", "1231321", "0000", "1234", "11", "12", "1")
isPalindrome(x)
# 0000000 1123456 1231321 0000 1234 11 12 1
# TRUE FALSE TRUE TRUE FALSE TRUE FALSE TRUE
In the following palindromFun function I'll add the actual palindroms as attributes so that they are being returned by the function. (To switch off this behavior just comment out the line with the ## mark).
palindromFun <- function(n1, n2) {
if (n1 > n2) {
print('n1 cannot be > n2')
} else {
tm <- sprintf("%06d", 0:235959)
dy <- n1:n2
r <- paste0(rep(dy, each=length(tm)), tm)
pd <- isPalindrome(r)
out <- sum(pd)
out <- `attr<-`(out, "palindroms", r[pd]) ## mark
return(out)
}
}
Result 1
r1 <- palindromFun(n1=0, n2=1)
r1
# [1] 472
# attr(,"palindroms")
# [1] "0000000" "0001000" "0002000" "0003000" "0004000" "0005000" "0006000"
# [8] "0007000" "0008000" "0009000" "0010100" "0011100" "0012100" "0013100"
# [15] "0014100" "0015100" "0016100" "0017100" "0018100" "0019100" "0020200"
# [22] "0021200" "0022200" "0023200" "0024200" "0025200" "0026200" "0027200"
# [29] "0028200" "0029200" "0030300" "0031300" "0032300" "0033300" "0034300"
# [36] "0035300" "0036300" "0037300" "0038300" "0039300" "0040400" "0041400"
# [43] "0042400" "0043400" "0044400" "0045400" "0046400" "0047400" "0048400"
# [50] "0049400" "0050500" "0051500" "0052500" "0053500" "0054500" "0055500"
# [57] "0056500" "0057500" "0058500" "0059500" "0060600" "0061600" "0062600"
# [64] "0063600" "0064600" "0065600" "0066600" "0067600" "0068600" "0069600"
# [71] "0070700" "0071700" "0072700" "0073700" "0074700" "0075700" "0076700"
# [78] "0077700" "0078700" "0079700" "0080800" "0081800" "0082800" "0083800"
# [85] "0084800" "0085800" "0086800" "0087800" "0088800" "0089800" "0090900"
# [92] "0091900" "0092900" "0093900" "0094900" "0095900" "0096900" "0097900"
# [99] "0098900" "0099900" "0100010" "0101010" "0102010" "0103010" "0104010"
# [106] "0105010" "0106010" "0107010" "0108010" "0109010" "0110110" "0111110"
# [113] "0112110" "0113110" "0114110" "0115110" "0116110" "0117110" "0118110"
# [120] "0119110" "0120210" "0121210" "0122210" "0123210" "0124210" "0125210"
# [127] "0126210" "0127210" "0128210" "0129210" "0130310" "0131310" "0132310"
# [134] "0133310" "0134310" "0135310" "0136310" "0137310" "0138310" "0139310"
# [141] "0140410" "0141410" "0142410" "0143410" "0144410" "0145410" "0146410"
# [148] "0147410" "0148410" "0149410" "0150510" "0151510" "0152510" "0153510"
# [155] "0154510" "0155510" "0156510" "0157510" "0158510" "0159510" "0160610"
# [162] "0161610" "0162610" "0163610" "0164610" "0165610" "0166610" "0167610"
# [169] "0168610" "0169610" "0170710" "0171710" "0172710" "0173710" "0174710"
# [176] "0175710" "0176710" "0177710" "0178710" "0179710" "0180810" "0181810"
# [183] "0182810" "0183810" "0184810" "0185810" "0186810" "0187810" "0188810"
# [190] "0189810" "0190910" "0191910" "0192910" "0193910" "0194910" "0195910"
# [197] "0196910" "0197910" "0198910" "0199910" "0200020" "0201020" "0202020"
# [204] "0203020" "0204020" "0205020" "0206020" "0207020" "0208020" "0209020"
# [211] "0210120" "0211120" "0212120" "0213120" "0214120" "0215120" "0216120"
# [218] "0217120" "0218120" "0219120" "0220220" "0221220" "0222220" "0223220"
# [225] "0224220" "0225220" "0226220" "0227220" "0228220" "0229220" "0230320"
# [232] "0231320" "0232320" "0233320" "0234320" "0235320" "1000001" "1001001"
# [239] "1002001" "1003001" "1004001" "1005001" "1006001" "1007001" "1008001"
# [246] "1009001" "1010101" "1011101" "1012101" "1013101" "1014101" "1015101"
# [253] "1016101" "1017101" "1018101" "1019101" "1020201" "1021201" "1022201"
# [260] "1023201" "1024201" "1025201" "1026201" "1027201" "1028201" "1029201"
# [267] "1030301" "1031301" "1032301" "1033301" "1034301" "1035301" "1036301"
# [274] "1037301" "1038301" "1039301" "1040401" "1041401" "1042401" "1043401"
# [281] "1044401" "1045401" "1046401" "1047401" "1048401" "1049401" "1050501"
# [288] "1051501" "1052501" "1053501" "1054501" "1055501" "1056501" "1057501"
# [295] "1058501" "1059501" "1060601" "1061601" "1062601" "1063601" "1064601"
# [302] "1065601" "1066601" "1067601" "1068601" "1069601" "1070701" "1071701"
# [309] "1072701" "1073701" "1074701" "1075701" "1076701" "1077701" "1078701"
# [316] "1079701" "1080801" "1081801" "1082801" "1083801" "1084801" "1085801"
# [323] "1086801" "1087801" "1088801" "1089801" "1090901" "1091901" "1092901"
# [330] "1093901" "1094901" "1095901" "1096901" "1097901" "1098901" "1099901"
# [337] "1100011" "1101011" "1102011" "1103011" "1104011" "1105011" "1106011"
# [344] "1107011" "1108011" "1109011" "1110111" "1111111" "1112111" "1113111"
# [351] "1114111" "1115111" "1116111" "1117111" "1118111" "1119111" "1120211"
# [358] "1121211" "1122211" "1123211" "1124211" "1125211" "1126211" "1127211"
# [365] "1128211" "1129211" "1130311" "1131311" "1132311" "1133311" "1134311"
# [372] "1135311" "1136311" "1137311" "1138311" "1139311" "1140411" "1141411"
# [379] "1142411" "1143411" "1144411" "1145411" "1146411" "1147411" "1148411"
# [386] "1149411" "1150511" "1151511" "1152511" "1153511" "1154511" "1155511"
# [393] "1156511" "1157511" "1158511" "1159511" "1160611" "1161611" "1162611"
# [400] "1163611" "1164611" "1165611" "1166611" "1167611" "1168611" "1169611"
# [407] "1170711" "1171711" "1172711" "1173711" "1174711" "1175711" "1176711"
# [414] "1177711" "1178711" "1179711" "1180811" "1181811" "1182811" "1183811"
# [421] "1184811" "1185811" "1186811" "1187811" "1188811" "1189811" "1190911"
# [428] "1191911" "1192911" "1193911" "1194911" "1195911" "1196911" "1197911"
# [435] "1198911" "1199911" "1200021" "1201021" "1202021" "1203021" "1204021"
# [442] "1205021" "1206021" "1207021" "1208021" "1209021" "1210121" "1211121"
# [449] "1212121" "1213121" "1214121" "1215121" "1216121" "1217121" "1218121"
# [456] "1219121" "1220221" "1221221" "1222221" "1223221" "1224221" "1225221"
# [463] "1226221" "1227221" "1228221" "1229221" "1230321" "1231321" "1232321"
# [470] "1233321" "1234321" "1235321"
Result 2
r2 <- palindromFun(n1=0, n2=2)
r2
# [1] 708
# attr(,"palindroms")
# [1] "0000000" "0001000" "0002000" "0003000" "0004000" "0005000" "0006000"
# [8] "0007000" "0008000" "0009000" "0010100" "0011100" "0012100" "0013100"
# [15] "0014100" "0015100" "0016100" "0017100" "0018100" "0019100" "0020200"
# [22] "0021200" "0022200" "0023200" "0024200" "0025200" "0026200" "0027200"
# [29] "0028200" "0029200" "0030300" "0031300" "0032300" "0033300" "0034300"
# [36] "0035300" "0036300" "0037300" "0038300" "0039300" "0040400" "0041400"
# [43] "0042400" "0043400" "0044400" "0045400" "0046400" "0047400" "0048400"
# [50] "0049400" "0050500" "0051500" "0052500" "0053500" "0054500" "0055500"
# [57] "0056500" "0057500" "0058500" "0059500" "0060600" "0061600" "0062600"
# [64] "0063600" "0064600" "0065600" "0066600" "0067600" "0068600" "0069600"
# [71] "0070700" "0071700" "0072700" "0073700" "0074700" "0075700" "0076700"
# [78] "0077700" "0078700" "0079700" "0080800" "0081800" "0082800" "0083800"
# [85] "0084800" "0085800" "0086800" "0087800" "0088800" "0089800" "0090900"
# [92] "0091900" "0092900" "0093900" "0094900" "0095900" "0096900" "0097900"
# [99] "0098900" "0099900" "0100010" "0101010" "0102010" "0103010" "0104010"
# [106] "0105010" "0106010" "0107010" "0108010" "0109010" "0110110" "0111110"
# [113] "0112110" "0113110" "0114110" "0115110" "0116110" "0117110" "0118110"
# [120] "0119110" "0120210" "0121210" "0122210" "0123210" "0124210" "0125210"
# [127] "0126210" "0127210" "0128210" "0129210" "0130310" "0131310" "0132310"
# [134] "0133310" "0134310" "0135310" "0136310" "0137310" "0138310" "0139310"
# [141] "0140410" "0141410" "0142410" "0143410" "0144410" "0145410" "0146410"
# [148] "0147410" "0148410" "0149410" "0150510" "0151510" "0152510" "0153510"
# [155] "0154510" "0155510" "0156510" "0157510" "0158510" "0159510" "0160610"
# [162] "0161610" "0162610" "0163610" "0164610" "0165610" "0166610" "0167610"
# [169] "0168610" "0169610" "0170710" "0171710" "0172710" "0173710" "0174710"
# [176] "0175710" "0176710" "0177710" "0178710" "0179710" "0180810" "0181810"
# [183] "0182810" "0183810" "0184810" "0185810" "0186810" "0187810" "0188810"
# [190] "0189810" "0190910" "0191910" "0192910" "0193910" "0194910" "0195910"
# [197] "0196910" "0197910" "0198910" "0199910" "0200020" "0201020" "0202020"
# [204] "0203020" "0204020" "0205020" "0206020" "0207020" "0208020" "0209020"
# [211] "0210120" "0211120" "0212120" "0213120" "0214120" "0215120" "0216120"
# [218] "0217120" "0218120" "0219120" "0220220" "0221220" "0222220" "0223220"
# [225] "0224220" "0225220" "0226220" "0227220" "0228220" "0229220" "0230320"
# [232] "0231320" "0232320" "0233320" "0234320" "0235320" "1000001" "1001001"
# [239] "1002001" "1003001" "1004001" "1005001" "1006001" "1007001" "1008001"
# [246] "1009001" "1010101" "1011101" "1012101" "1013101" "1014101" "1015101"
# [253] "1016101" "1017101" "1018101" "1019101" "1020201" "1021201" "1022201"
# [260] "1023201" "1024201" "1025201" "1026201" "1027201" "1028201" "1029201"
# [267] "1030301" "1031301" "1032301" "1033301" "1034301" "1035301" "1036301"
# [274] "1037301" "1038301" "1039301" "1040401" "1041401" "1042401" "1043401"
# [281] "1044401" "1045401" "1046401" "1047401" "1048401" "1049401" "1050501"
# [288] "1051501" "1052501" "1053501" "1054501" "1055501" "1056501" "1057501"
# [295] "1058501" "1059501" "1060601" "1061601" "1062601" "1063601" "1064601"
# [302] "1065601" "1066601" "1067601" "1068601" "1069601" "1070701" "1071701"
# [309] "1072701" "1073701" "1074701" "1075701" "1076701" "1077701" "1078701"
# [316] "1079701" "1080801" "1081801" "1082801" "1083801" "1084801" "1085801"
# [323] "1086801" "1087801" "1088801" "1089801" "1090901" "1091901" "1092901"
# [330] "1093901" "1094901" "1095901" "1096901" "1097901" "1098901" "1099901"
# [337] "1100011" "1101011" "1102011" "1103011" "1104011" "1105011" "1106011"
# [344] "1107011" "1108011" "1109011" "1110111" "1111111" "1112111" "1113111"
# [351] "1114111" "1115111" "1116111" "1117111" "1118111" "1119111" "1120211"
# [358] "1121211" "1122211" "1123211" "1124211" "1125211" "1126211" "1127211"
# [365] "1128211" "1129211" "1130311" "1131311" "1132311" "1133311" "1134311"
# [372] "1135311" "1136311" "1137311" "1138311" "1139311" "1140411" "1141411"
# [379] "1142411" "1143411" "1144411" "1145411" "1146411" "1147411" "1148411"
# [386] "1149411" "1150511" "1151511" "1152511" "1153511" "1154511" "1155511"
# [393] "1156511" "1157511" "1158511" "1159511" "1160611" "1161611" "1162611"
# [400] "1163611" "1164611" "1165611" "1166611" "1167611" "1168611" "1169611"
# [407] "1170711" "1171711" "1172711" "1173711" "1174711" "1175711" "1176711"
# [414] "1177711" "1178711" "1179711" "1180811" "1181811" "1182811" "1183811"
# [421] "1184811" "1185811" "1186811" "1187811" "1188811" "1189811" "1190911"
# [428] "1191911" "1192911" "1193911" "1194911" "1195911" "1196911" "1197911"
# [435] "1198911" "1199911" "1200021" "1201021" "1202021" "1203021" "1204021"
# [442] "1205021" "1206021" "1207021" "1208021" "1209021" "1210121" "1211121"
# [449] "1212121" "1213121" "1214121" "1215121" "1216121" "1217121" "1218121"
# [456] "1219121" "1220221" "1221221" "1222221" "1223221" "1224221" "1225221"
# [463] "1226221" "1227221" "1228221" "1229221" "1230321" "1231321" "1232321"
# [470] "1233321" "1234321" "1235321" "2000002" "2001002" "2002002" "2003002"
# [477] "2004002" "2005002" "2006002" "2007002" "2008002" "2009002" "2010102"
# [484] "2011102" "2012102" "2013102" "2014102" "2015102" "2016102" "2017102"
# [491] "2018102" "2019102" "2020202" "2021202" "2022202" "2023202" "2024202"
# [498] "2025202" "2026202" "2027202" "2028202" "2029202" "2030302" "2031302"
# [505] "2032302" "2033302" "2034302" "2035302" "2036302" "2037302" "2038302"
# [512] "2039302" "2040402" "2041402" "2042402" "2043402" "2044402" "2045402"
# [519] "2046402" "2047402" "2048402" "2049402" "2050502" "2051502" "2052502"
# [526] "2053502" "2054502" "2055502" "2056502" "2057502" "2058502" "2059502"
# [533] "2060602" "2061602" "2062602" "2063602" "2064602" "2065602" "2066602"
# [540] "2067602" "2068602" "2069602" "2070702" "2071702" "2072702" "2073702"
# [547] "2074702" "2075702" "2076702" "2077702" "2078702" "2079702" "2080802"
# [554] "2081802" "2082802" "2083802" "2084802" "2085802" "2086802" "2087802"
# [561] "2088802" "2089802" "2090902" "2091902" "2092902" "2093902" "2094902"
# [568] "2095902" "2096902" "2097902" "2098902" "2099902" "2100012" "2101012"
# [575] "2102012" "2103012" "2104012" "2105012" "2106012" "2107012" "2108012"
# [582] "2109012" "2110112" "2111112" "2112112" "2113112" "2114112" "2115112"
# [589] "2116112" "2117112" "2118112" "2119112" "2120212" "2121212" "2122212"
# [596] "2123212" "2124212" "2125212" "2126212" "2127212" "2128212" "2129212"
# [603] "2130312" "2131312" "2132312" "2133312" "2134312" "2135312" "2136312"
# [610] "2137312" "2138312" "2139312" "2140412" "2141412" "2142412" "2143412"
# [617] "2144412" "2145412" "2146412" "2147412" "2148412" "2149412" "2150512"
# [624] "2151512" "2152512" "2153512" "2154512" "2155512" "2156512" "2157512"
# [631] "2158512" "2159512" "2160612" "2161612" "2162612" "2163612" "2164612"
# [638] "2165612" "2166612" "2167612" "2168612" "2169612" "2170712" "2171712"
# [645] "2172712" "2173712" "2174712" "2175712" "2176712" "2177712" "2178712"
# [652] "2179712" "2180812" "2181812" "2182812" "2183812" "2184812" "2185812"
# [659] "2186812" "2187812" "2188812" "2189812" "2190912" "2191912" "2192912"
# [666] "2193912" "2194912" "2195912" "2196912" "2197912" "2198912" "2199912"
# [673] "2200022" "2201022" "2202022" "2203022" "2204022" "2205022" "2206022"
# [680] "2207022" "2208022" "2209022" "2210122" "2211122" "2212122" "2213122"
# [687] "2214122" "2215122" "2216122" "2217122" "2218122" "2219122" "2220222"
# [694] "2221222" "2222222" "2223222" "2224222" "2225222" "2226222" "2227222"
# [701] "2228222" "2229222" "2230322" "2231322" "2232322" "2233322" "2234322"
# [708] "2235322"
My number of palindroms seems to be different from yours, though.
Here is a quick method to create the desired sequence using R's builtin time and date functions.
#create the time sequence for every second for 1 day
dateseq <- seq(as.POSIXct("2020-08-15"), as.POSIXct("2020-08-16"), by="1 sec")
#remove the last element (midnight the next day)
dateseq <- dateseq[-86401]
#format the desire
answer <- format(dateseq, "%H%M%S")
tail(answer)
#[1] "235954" "235955" "235956" "235957" "235958" "235959"
Here's one way to approach the entire problem using a functional approach, using only base R. That is, breaking each problem down to a single task and building up the functionality you need:
# Converts strings in the format "1234556" to date times
as_time <- function(chr) {
chr[nchar(chr) == 7] <- paste0("0", chr[nchar(chr) == 7])
strptime(chr, "%d%H%M%S")
}
# Converts date-times to strings in format "1234556"
as_chr <- function(t) {
paste0(as.numeric(substr(t, 9, 10)), strftime(t, "%H%M%S"))
}
# Gets a sequence of valid strings between to strings in format "1234556"
seq_times <- function(t1, t2)
{
as_chr(seq(as_time(t1), as_time(t2), by = "1 sec"))
}
# Reverse strings in a character vector
rev_string <- function(s) {
sapply(s, function(x) intToUtf8(rev(utf8ToInt(x))), USE.NAMES = FALSE)
}
# Returns only the subset of a given character vector that are palindromes
get_palindromes <- function(t1, t2) {
str <- seq_times(t1, t2)
str[str == rev_string(str)]
}
So now we can do:
get_palindromes("1000000", "2000000")
#> [1] "1000001" "1001001" "1002001" "1003001" "1004001" "1005001" "1010101"
#> [8] "1011101" "1012101" "1013101" "1014101" "1015101" "1020201" "1021201"
#> [15] "1022201" "1023201" "1024201" "1025201" "1030301" "1031301" "1032301"
#> [22] "1033301" "1034301" "1035301" "1040401" "1041401" "1042401" "1043401"
#> [29] "1044401" "1045401" "1050501" "1051501" "1052501" "1053501" "1054501"
#> [36] "1055501" "1060601" "1061601" "1062601" "1063601" "1064601" "1065601"
#> [43] "1070701" "1071701" "1072701" "1073701" "1074701" "1075701" "1080801"
#> [50] "1081801" "1082801" "1083801" "1084801" "1085801" "1090901" "1091901"
#> [57] "1092901" "1093901" "1094901" "1095901" "1100011" "1101011" "1102011"
#> [64] "1103011" "1104011" "1105011" "1110111" "1111111" "1112111" "1113111"
#> [71] "1114111" "1115111" "1120211" "1121211" "1122211" "1123211" "1124211"
#> [78] "1125211" "1130311" "1131311" "1132311" "1133311" "1134311" "1135311"
#> [85] "1140411" "1141411" "1142411" "1143411" "1144411" "1145411" "1150511"
#> [92] "1151511" "1152511" "1153511" "1154511" "1155511" "1160611" "1161611"
#> [99] "1162611" "1163611" "1164611" "1165611" "1170711" "1171711" "1172711"
#> [106] "1173711" "1174711" "1175711" "1180811" "1181811" "1182811" "1183811"
#> [113] "1184811" "1185811" "1190911" "1191911" "1192911" "1193911" "1194911"
#> [120] "1195911" "1200021" "1201021" "1202021" "1203021" "1204021" "1205021"
#> [127] "1210121" "1211121" "1212121" "1213121" "1214121" "1215121" "1220221"
#> [134] "1221221" "1222221" "1223221" "1224221" "1225221" "1230321" "1231321"
#> [141] "1232321" "1233321" "1234321" "1235321"
and
get_palindromes("2235000", "3060000")
#> [1] "2235322" "3000003" "3001003" "3002003" "3003003" "3004003" "3005003"
#> [8] "3010103" "3011103" "3012103" "3013103" "3014103" "3015103" "3020203"
#> [15] "3021203" "3022203" "3023203" "3024203" "3025203" "3030303" "3031303"
#> [22] "3032303" "3033303" "3034303" "3035303" "3040403" "3041403" "3042403"
#> [29] "3043403" "3044403" "3045403" "3050503" "3051503" "3052503" "3053503"
#> [36] "3054503" "3055503"
What do you mean by the length? If you mean the count then I think we can use of simple math to see how many possibilities are there.
Let us say for n1 = 1 and n2 =2, out of 7 places available(dhhmmss), you can have only 2 choices for the 1st and the 7th place. Now for the remaining 6 places, we need to think only about first 3 places as the rest of them will be same as the first three( by the palindrome logic).
Now for the 2nd place, we can have only 3 choices(0, 1, 2 as we can only have the hour from 00 to 23, just consider the ten's place). Let us store the value at the 2nd place to a variable h. Next, we have 3rd place which can have 10, 10 and 4 choices for h={0,1,2} respectively. Following that, we have the 4th place which can only have 6 choices( ranging from 00 to 59,here just the ten's place).
Hence, the total choices are 2*[10+10+4]*6 = 288 choices.
You can use rep() to create the various time elements (days, hours,etc) and then expand.grid() to get every combination of the elements. stri_reverse() from stringi can be used to compare the reverse of the string and thus establish if it is a palindrome.
find_palindrome<-function(day_start,day_end){
day<-rep(day_start:day_end)
hour<-rep(0:23)
min_sec<-rep(0:59)
#expand.grid() finds every combination of inputs
#min_sec is used twice within expand.grid(), once for minutes and once for seconds.
# The "%02d" within sprint() preserves a 2-digit length (e.g. '01' instead of '1'.)
df<-expand.grid(day, sprintf("%02d",hour), sprintf("%02d",min_sec), sprintf("%02d",min_sec))
df<-as.data.frame(df)
#create a column concatinating the values
df$compare1<-paste(df[,1],df[,2], df[,3], df[,4], sep="")
#reverse the order in another column
df$compare2<-stringi::stri_reverse(df$compare1)
#compare the numbers to find your palendromes
palindrone<-df$compare1[df$compare1 == df$compare2]
return(palindrone)
}
Then run the function:
#example using day 0 to day 2
find_palindrome(0,2)

R time interval

I have time array;
time_array
[1] 08:05:02 08:46:08 09:13:54 09:51:21 10:07:31 11:34:12
[7] 11:45:28 12:18:14 12:26:05 12:58:35 13:11:09 14:14:29
[13] 15:28:47 15:56:47 16:22:06 16:40:15 18:15:00 19:01:39
[19] 20:36:22 21:16:27 21:26:11 21:43:06 23:51:47 00:18:35
[25] 01:02:37 02:08:14 02:52:09 04:02:01 04:37:46 05:41:58
[31] 05:59:22 07:22:07 08:32:47 09:13:23 09:39:01 10:19:35
[37] 11:53:18 12:05:53 12:18:42 12:33:04 13:16:19 13:37:34
[43] 13:54:14 14:31:39 14:44:46 15:26:23 16:03:25 17:21:44
[49] 18:00:24 19:10:50 19:44:01 20:55:16 21:09:06 22:02:01
[55] 22:53:00 23:19:43 23:59:01 01:39:20 02:28:35 02:41:08
[61] 02:57:42 03:03:45 03:10:13 04:57:44 06:00:59 07:51:50
[67] 08:10:19 08:23:24 08:32:52 09:37:47 10:43:16 11:17:07
I need to do some control on these values. For example;
if(time_array[i]>"08:05:00" | time_array[i]<"08:15:00")){
cond[i]<-1
}else{
cond[i]<-0
}
How can I do this?
We can do something like this in base R:
# Convert to POSIXct
times <- as.POSIXct(time_array, format = "%H:%M:%S")
# Store in data.frame and flag entries
df <- data.frame(
time = time_array,
flag = as.numeric(
times > as.POSIXct("08:05:00", format = "%H:%M:%S") &
times < as.POSIXct("08:15:00", format = "%H:%M:%S")))
head(df);
# time flag
#1 08:05:02 1
#2 08:46:08 0
#3 09:13:54 0
#4 09:51:21 0
#5 10:07:31 0
#6 11:34:12 0
Sample data
time_array <- c(
'08:05:02', '08:46:08', '09:13:54', '09:51:21', '10:07:31', '11:34:12',
'11:45:28', '12:18:14', '12:26:05', '12:58:35', '13:11:09', '14:14:29',
'15:28:47', '15:56:47', '16:22:06', '16:40:15', '18:15:00', '19:01:39',
'20:36:22', '21:16:27', '21:26:11', '21:43:06', '23:51:47', '00:18:35',
'01:02:37', '02:08:14', '02:52:09', '04:02:01', '04:37:46', '05:41:58',
'05:59:22', '07:22:07', '08:32:47', '09:13:23', '09:39:01', '10:19:35',
'11:53:18', '12:05:53', '12:18:42', '12:33:04', '13:16:19', '13:37:34',
'13:54:14', '14:31:39', '14:44:46', '15:26:23', '16:03:25', '17:21:44',
'18:00:24', '19:10:50', '19:44:01', '20:55:16', '21:09:06', '22:02:01',
'22:53:00', '23:19:43', '23:59:01', '01:39:20', '02:28:35', '02:41:08',
'02:57:42', '03:03:45', '03:10:13', '04:57:44', '06:00:59', '07:51:50',
'08:10:19', '08:23:24', '08:32:52', '09:37:47', '10:43:16', '11:17:07')

easy solution needed to subset spectra files in list.files

I have a folder full of spectra files. The number of files can vary with different measurements as well as the repetitions.
I have so far and it works:
files <- list.files(pattern = "^Q\\d+")
print(files)
and print(list) gives:
[1] "Q010101N.001" "Q010101N.002" "Q010101N.003" "Q010101N.004" "Q010101N.005" "Q010101N.006"
[7] "Q010101N.007" "Q010101N.008" "Q010101N.009" "Q010101N.010" "Q010101N.011" "Q010101N.012"
[13] "Q010101N.013" "Q010101N.014" "Q010101N.015" "Q010101N.016" "Q010101N.017" "Q010101N.018"
[19] "Q010101N.019" "Q010101N.020" "Q010101N.021" "Q010101N.022" "Q010101N.023" "Q010101N.024"
[25] "Q010101N.025" "Q021101N.001" "Q021101N.002" "Q021101N.003" "Q021101N.004" "Q021101N.005"
[31] "Q021101N.006" "Q021101N.007" "Q021101N.008" "Q021101N.009" "Q021101N.010" "Q021101N.011"
[37] "Q021101N.012" "Q021101N.013" "Q021101N.014" "Q021101N.015" "Q021101N.016" "Q021101N.017"
[43] "Q021101N.018" "Q021101N.019" "Q021101N.020" "Q021101N.021" "Q021101N.022" "Q021101N.023"
[49] "Q021101N.024" "Q021101N.025" "Q031201N.001" "Q031201N.002" "Q031201N.003" "Q031201N.004"
[55] "Q031201N.005" "Q031201N.006" "Q031201N.007" "Q031201N.008" "Q031201N.009" "Q031201N.010"
[61] "Q031201N.011" "Q031201N.012" "Q031201N.013" "Q031201N.014" "Q031201N.015" "Q031201N.016"
[67] "Q031201N.017" "Q031201N.018" "Q031201N.019" "Q031201N.020" "Q031201N.021" "Q031201N.022"
[73] "Q031201N.023" "Q031201N.024" "Q031201N.025" "Q041301N.001" "Q041301N.002" "Q041301N.003"
[79] "Q041301N.004" "Q041301N.005" "Q041301N.006" "Q041301N.007" "Q041301N.008" "Q041301N.009"
[85] "Q041301N.010" "Q041301N.011" "Q041301N.012" "Q041301N.013" "Q041301N.014" "Q041301N.015"
[91] "Q041301N.016" "Q041301N.017" "Q041301N.018" "Q041301N.019" "Q041301N.020" "Q041301N.021"
[97] "Q041301N.022" "Q041301N.023" "Q041301N.024" "Q041301N.025" "Q051401N.001" "Q051401N.002"
[103] "Q051401N.003" "Q051401N.004" "Q051401N.005" "Q051401N.006" "Q051401N.007" "Q051401N.008"
[109] "Q051401N.009" "Q051401N.010" "Q051401N.011" "Q051401N.012" "Q051401N.013" "Q051401N.014"
[115] "Q051401N.015" "Q051401N.016" "Q051401N.017" "Q051401N.018" "Q051401N.019" "Q051401N.020"
[121] "Q051401N.021" "Q051401N.022" "Q051401N.023" "Q051401N.024" "Q051401N.025" "Q061501N.001"
[127] "Q061501N.002" "Q061501N.003" "Q061501N.004" "Q061501N.005" "Q061501N.006" "Q061501N.007"
[133] "Q061501N.008" "Q061501N.009" "Q061501N.010" "Q061501N.011" "Q061501N.012" "Q061501N.013"
[139] "Q061501N.014" "Q061501N.015" "Q061501N.016" "Q061501N.017" "Q061501N.018" "Q061501N.019"
[145] "Q061501N.020" "Q061501N.021" "Q061501N.022" "Q061501N.023" "Q061501N.024" "Q061501N.025"
[151] "Q071601N.001" "Q071601N.002" "Q071601N.003" "Q071601N.004" "Q071601N.005" "Q071601N.006"
[157] "Q071601N.007" "Q071601N.008" "Q071601N.009" "Q071601N.010" "Q071601N.011" "Q071601N.012"
[163] "Q071601N.013" "Q071601N.014" "Q071601N.015" "Q071601N.016" "Q071601N.017" "Q071601N.018"
[169] "Q071601N.019" "Q071601N.020" "Q071601N.021" "Q071601N.022" "Q071601N.023" "Q071601N.024"
[175] "Q071601N.025" "Q081701N.001" "Q081701N.002" "Q081701N.003" "Q081701N.004" "Q081701N.005"
[181] "Q081701N.006" "Q081701N.007" "Q081701N.008" "Q081701N.009" "Q081701N.010" "Q081701N.011"
[187] "Q081701N.012" "Q081701N.013" "Q081701N.014" "Q081701N.015" "Q081701N.016" "Q081701N.017"
[193] "Q081701N.018" "Q081701N.019" "Q081701N.020" "Q081701N.021" "Q081701N.022" "Q081701N.023"
[199] "Q081701N.024" "Q081701N.025" "Q091801N.001" "Q091801N.002" "Q091801N.003" "Q091801N.004"
[205] "Q091801N.005" "Q091801N.006" "Q091801N.007" "Q091801N.008" "Q091801N.009" "Q091801N.010"
[211] "Q091801N.011" "Q091801N.012" "Q091801N.013" "Q091801N.014" "Q091801N.015" "Q091801N.016"
[217] "Q091801N.017" "Q091801N.018" "Q091801N.019" "Q091801N.020" "Q091801N.021" "Q091801N.022"
[223] "Q091801N.023" "Q091801N.024" "Q091801N.025" "Q101901N.001" "Q101901N.002" "Q101901N.003"
[229] "Q101901N.004" "Q101901N.005" "Q101901N.006" "Q101901N.007" "Q101901N.008" "Q101901N.009"
[235] "Q101901N.010" "Q101901N.011" "Q101901N.012" "Q101901N.013" "Q101901N.014" "Q101901N.015"
[241] "Q101901N.016" "Q101901N.017" "Q101901N.018" "Q101901N.019" "Q101901N.020" "Q101901N.021"
[247] "Q101901N.022" "Q101901N.023" "Q101901N.024" "Q101901N.025" "Q112001N.001" "Q112001N.002"
[253] "Q112001N.003" "Q112001N.004" "Q112001N.005" "Q112001N.006" "Q112001N.007" "Q112001N.008"
[259] "Q112001N.009" "Q112001N.010" "Q112001N.011" "Q112001N.012" "Q112001N.013" "Q112001N.014"
[265] "Q112001N.015" "Q112001N.016" "Q112001N.017" "Q112001N.018" "Q112001N.019" "Q112001N.020"
[271] "Q112001N.021" "Q112001N.022" "Q112001N.023" "Q112001N.024" "Q112001N.025" "Q124101N.001"
[277] "Q124101N.002" "Q124101N.003" "Q124101N.004" "Q124101N.005" "Q124101N.006" "Q124101N.007"
[283] "Q124101N.008" "Q124101N.009" "Q124101N.010" "Q124101N.011" "Q124101N.012" "Q124101N.013"
[289] "Q124101N.014" "Q124101N.015" "Q124101N.016" "Q124101N.017" "Q124101N.018" "Q124101N.019"
[295] "Q124101N.020" "Q124101N.021" "Q124101N.022" "Q124101N.023" "Q124101N.024" "Q124101N.025"
[301] "Q134201N.001" "Q134201N.002" "Q134201N.003" "Q134201N.004" "Q134201N.005" "Q134201N.006"
[307] "Q134201N.007" "Q134201N.008" "Q134201N.009" "Q134201N.010" "Q134201N.011" "Q134201N.012"
[313] "Q134201N.013" "Q134201N.014" "Q134201N.015" "Q134201N.016" "Q134201N.017" "Q134201N.018"
[319] "Q134201N.019" "Q134201N.020" "Q134201N.021" "Q134201N.022" "Q134201N.023" "Q134201N.024"
[325] "Q134201N.025" "Q144301N.001" "Q144301N.002" "Q144301N.003" "Q144301N.004" "Q144301N.005"
[331] "Q144301N.006" "Q144301N.007" "Q144301N.008" "Q144301N.009" "Q144301N.010" "Q144301N.011"
[337] "Q144301N.012" "Q144301N.013" "Q144301N.014" "Q144301N.015" "Q144301N.016" "Q144301N.017"
[343] "Q144301N.018" "Q144301N.019" "Q144301N.020" "Q144301N.021" "Q144301N.022" "Q144301N.023"
[349] "Q144301N.024" "Q144301N.025" "Q154401N.001" "Q154401N.002" "Q154401N.003" "Q154401N.004"
[355] "Q154401N.005" "Q154401N.006" "Q154401N.007" "Q154401N.008" "Q154401N.009" "Q154401N.010"
[361] "Q154401N.011" "Q154401N.012" "Q154401N.013" "Q154401N.014" "Q154401N.015" "Q154401N.016"
[367] "Q154401N.017" "Q154401N.018" "Q154401N.019" "Q154401N.020" "Q154401N.021" "Q154401N.022"
[373] "Q154401N.023" "Q154401N.024" "Q154401N.025" "Q164501N.001" "Q164501N.002" "Q164501N.003"
[379] "Q164501N.004" "Q164501N.005" "Q164501N.006" "Q164501N.007" "Q164501N.008" "Q164501N.009"
[385] "Q164501N.010" "Q164501N.011" "Q164501N.012" "Q164501N.013" "Q164501N.014" "Q164501N.015"
[391] "Q164501N.016" "Q164501N.017" "Q164501N.018" "Q164501N.019" "Q164501N.020" "Q164501N.021"
[397] "Q164501N.022" "Q164501N.023" "Q164501N.024" "Q164501N.025" "Q174601N.001" "Q174601N.002"
[403] "Q174601N.003" "Q174601N.004" "Q174601N.005" "Q174601N.006" "Q174601N.007" "Q174601N.008"
[409] "Q174601N.009" "Q174601N.010" "Q174601N.011" "Q174601N.012" "Q174601N.013" "Q174601N.014"
[415] "Q174601N.015" "Q174601N.016" "Q174601N.017" "Q174601N.018" "Q174601N.019" "Q174601N.020"
[421] "Q174601N.021" "Q174601N.022" "Q174601N.023" "Q174601N.024" "Q174601N.025"
So in this case I get 425 spectra files and there are 25 repetitions of each sample. However the total number of files could be different another time and it could also be that one sample has 10 repetitions and the rest has 14 for example.
So I would like to subset each sample (with its repetitions into one subset). In this case I would get 17 subsets.
And the I need to import the files, which I have done succesfully before with all spectra files:
list.data <- list()
#import all spectra files
for (i in 1:length(files))
list.data[[i]] <- read.csv(files[i])
Given that I have now subsets, that would be slightly different!?
You can do this via a helper function and iteration. I used dplyr, purrr and stringi. This will put all your files into a single dataframe. After that you can manipulate it as you see fit.
library(dplyr)
library(purrr)
library(stringi)
read_spectra <- function(file){
file_name <- basename(file)
read.csv(file) %>%
mutate(sample = stri_extract_first_regex(file_name, "([A-Z][0-9]+)(?=.)"),
repetition = stri_extract_first_regex(file_name, "(?<=\\.)(\\d+)")) %>%
select(sample, repetition, everything())
}
full_data <- map_df(files, read_spectra)
The helper function:
Takes a file from list.files.
Reads the csv.
Uses mutate to make two new columns using regex to extract the sample number and repetition.
Orders the columns into sample, repetition and everything else.
The iteration is using map_df() from purrr to iterate read_spectra over each file in files and bind all of this together into a dataframe.

Finding first N consecutive composite numbers

The following is guaranteed to return N consecutive composite numbers:
(N+1)!+2,(N+1)!+3........(N+1)!+(N+1)
I used this to find 5 consecutive composite numbers in R using:
N=5
for(i in 2:6){a=factorial(N+1)+i;print(a);}
# [1] 722
# [1] 723
# [1] 724
# [1] 725
# [1] 726
However, I want first 'N' consecutive composite numbers, which this code is not guaranteed to return. For instance, for N=5, I want 24,25,26,27,28.
You can generate a list of primes with numbers:::Primes or numbers:::primeSieve (thanks to #Nicola for pointing to this function in a comment!), compute the gaps between each with diff, and then return a sequence from the first prime number whose gap is at least N with seq:
library(numbers)
primes <- as.integer(numbers:::primeSieve(100000000)) # About 9 seconds
d <- diff(primes)
firstNComposite <- function(N) {
valid <- which(d >= N+1)
if (length(valid) == 0) {
stop("Need to generate more prime numbers")
} else {
seq(primes[valid[1]]+1, length.out=N)
}
}
firstNComposite(5)
# [1] 24 25 26 27 28
firstNComposite(200)
# [1] 20831324 20831325 20831326 20831327 20831328 20831329 20831330 20831331 20831332 20831333 20831334
# [12] 20831335 20831336 20831337 20831338 20831339 20831340 20831341 20831342 20831343 20831344 20831345
# [23] 20831346 20831347 20831348 20831349 20831350 20831351 20831352 20831353 20831354 20831355 20831356
# [34] 20831357 20831358 20831359 20831360 20831361 20831362 20831363 20831364 20831365 20831366 20831367
# [45] 20831368 20831369 20831370 20831371 20831372 20831373 20831374 20831375 20831376 20831377 20831378
# [56] 20831379 20831380 20831381 20831382 20831383 20831384 20831385 20831386 20831387 20831388 20831389
# [67] 20831390 20831391 20831392 20831393 20831394 20831395 20831396 20831397 20831398 20831399 20831400
# [78] 20831401 20831402 20831403 20831404 20831405 20831406 20831407 20831408 20831409 20831410 20831411
# [89] 20831412 20831413 20831414 20831415 20831416 20831417 20831418 20831419 20831420 20831421 20831422
# [100] 20831423 20831424 20831425 20831426 20831427 20831428 20831429 20831430 20831431 20831432 20831433
# [111] 20831434 20831435 20831436 20831437 20831438 20831439 20831440 20831441 20831442 20831443 20831444
# [122] 20831445 20831446 20831447 20831448 20831449 20831450 20831451 20831452 20831453 20831454 20831455
# [133] 20831456 20831457 20831458 20831459 20831460 20831461 20831462 20831463 20831464 20831465 20831466
# [144] 20831467 20831468 20831469 20831470 20831471 20831472 20831473 20831474 20831475 20831476 20831477
# [155] 20831478 20831479 20831480 20831481 20831482 20831483 20831484 20831485 20831486 20831487 20831488
# [166] 20831489 20831490 20831491 20831492 20831493 20831494 20831495 20831496 20831497 20831498 20831499
# [177] 20831500 20831501 20831502 20831503 20831504 20831505 20831506 20831507 20831508 20831509 20831510
# [188] 20831511 20831512 20831513 20831514 20831515 20831516 20831517 20831518 20831519 20831520 20831521
# [199] 20831522 20831523

R - difference between sums of consecutive pairs

I have used the diff(x,) function on a data set to find the difference between force for time intervals of 0.0003s. The problem is due to this high sampling frequencies and the measuring inaccuracies of the system this does not yield any meaningful data.
I am wondering if it was possible to find the difference between 100 consecutive numbers. For example "difference = (Fz101+Fz102...Fz200)-(Fz1+Fz2...Fz100)
currently i am dividing by 0.000333 (sampling frequency) since i am trying to find the peak value for (delta Fz)/(delta t.)
(max(diff(lift_S8_Intns40_chainno$Fz)))/0.00033333
[1] 40472.2
Seen a similiar question asked if this helps anyone in helping me.
[link to similiar question][1]
How to find the difference between sums of consecutive pairs and triplets in R?
Any help is appreciated but my skills are not good in r so please make it as simple as possible.
Data structure
structure(list(Time = c(18.31266667, 18.313, 18.31333333, 18.31366667,
18.314, 18.31433333, 18.31466667, 18.315, 18.31533333, 18.31566667,
18.316, 18.31633333, 18.31666667, 18.317, 18.31733333, 18.31766667,
18.318, 18.31833333, 18.31866667, 18.319, 18.31933333, 18.31966667,
18.32, 18.32033333, 18.32066667, 18.321, 18.32133333, 18.32166667,
18.322, 18.32233333, 18.32266667, 18.323, 18.32333333, 18.32366667,
18.324, 18.32433333, 18.32466667, 18.325, 18.32533333, 18.32566667,
18.326, 18.32633333, 18.32666667, 18.327, 18.32733333, 18.32766667,
18.328, 18.32833333, 18.32866667, 18.329, 18.32933333, 18.32966667,
18.33, 18.33033333, 18.33066667, 18.331, 18.33133333, 18.33166667,
18.332, 18.33233333, 18.33266667, 18.333, 18.33333333, 18.33366667,
18.334, 18.33433333, 18.33466667, 18.335, 18.33533333, 18.33566667,
18.336, 18.33633333, 18.33666667, 18.337, 18.33733333, 18.33766667,
18.338, 18.33833333, 18.33866667, 18.339, 18.33933333, 18.33966667,
18.34, 18.34033333, 18.34066667, 18.341, 18.34133333, 18.34166667,
18.342, 18.34233333, 18.34266667, 18.343, 18.34333333, 18.34366667,
18.344, 18.34433333, 18.34466667, 18.345, 18.34533333, 18.34566667,
18.346, 18.34633333, 18.34666667, 18.347, 18.34733333, 18.34766667,
18.348, 18.34833333, 18.34866667, 18.349, 18.34933333, 18.34966667,
18.35, 18.35033333, 18.35066667, 18.351, 18.35133333, 18.35166667,
18.352, 18.35233333, 18.35266667, 18.353, 18.35333333, 18.35366667,
18.354, 18.35433333, 18.35466667, 18.355, 18.35533333, 18.35566667,
18.356, 18.35633333, 18.35666667, 18.357, 18.35733333, 18.35766667,
18.358, 18.35833333, 18.35866667, 18.359, 18.35933333, 18.35966667,
18.36, 18.36033333, 18.36066667, 18.361, 18.36133333, 18.36166667,
18.362, 18.36233333, 18.36266667, 18.363, 18.36333333, 18.36366667,
18.364, 18.36433333, 18.36466667, 18.365, 18.36533333, 18.36566667,
18.366, 18.36633333, 18.36666667, 18.367, 18.36733333, 18.36766667,
18.368, 18.36833333, 18.36866667, 18.369, 18.36933333, 18.36966667,
18.37, 18.37033333, 18.37066667, 18.371, 18.37133333, 18.37166667,
18.372, 18.37233333, 18.37266667, 18.373, 18.37333333, 18.37366667,
18.374, 18.37433333, 18.37466667, 18.375, 18.37533333, 18.37566667,
18.376, 18.37633333, 18.37666667, 18.377, 18.37733333, 18.37766667,
18.378, 18.37833333, 18.37866667, 18.379), Fz = c(2751.996392,
2751.614101, 2752.364284, 2751.993635, 2751.615723, 2751.995127,
2751.989063, 2751.241442, 2749.366648, 2750.486962, 2748.242022,
2747.498762, 2748.245589, 2747.4998, 2748.254604, 2748.631317,
2749.006798, 2750.129349, 2749.38176, 2749.385262, 2748.640657,
2750.13658, 2748.640073, 2749.393888, 2750.145076, 2751.274421,
2750.157172, 2750.15688, 2750.165279, 2752.407479, 2752.042472,
2753.550297, 2752.424309, 2750.927558, 2751.688556, 2751.317583,
2752.819117, 2752.45398, 2752.836757, 2752.472626, 2754.353062,
2754.725527, 2755.111158, 2754.376021, 2752.882448, 2752.510924,
2754.006329, 2755.508722, 2756.638796, 2756.636202, 2757.76947,
2756.284199, 2757.408096, 2758.165381, 2757.418894, 2758.172774,
2758.178838, 2758.936415, 2758.56421, 2757.448825, 2757.824079,
2758.569203, 2759.69824, 2760.082914, 2759.711827, 2760.840004,
2761.215128, 2760.84743, 2760.099193, 2760.851808, 2759.359468,
2758.25031, 2758.624201, 2759.755281, 2760.511577, 2761.635327,
2763.137948, 2763.143331, 2761.64191, 2763.1528, 2762.406524,
2763.525491, 2763.157339, 2762.781356, 2763.909695, 2764.666851,
2763.919813, 2763.543975, 2763.928017, 2765.045866, 2765.426243,
2766.182961, 2765.058383, 2763.936351, 2765.06542, 2763.947052,
2764.699019, 2764.70424, 2763.584867, 2763.22145, 2763.975492,
2764.349189, 2764.354994, 2763.613307, 2760.9942, 2759.878475,
2762.486297, 2762.115389, 2763.994527, 2762.121615, 2762.876597,
2761.391148, 2761.391991, 2761.768055, 2761.769126, 2759.525662,
2758.406516, 2758.786634, 2758.783261, 2757.669758, 2756.920758,
2756.540737, 2756.545277, 2756.176055, 2756.171515, 2755.423667,
2754.67611, 2753.178257, 2752.423923, 2753.16895, 2748.678277,
2747.927494, 2746.429754, 2746.424955, 2746.797971, 2746.424663,
2745.674982, 2746.424792, 2743.432215, 2743.432994, 2741.179315,
2742.67553, 2739.672447, 2738.921647, 2738.173637, 2737.424005,
2736.294822, 2735.180735, 2734.431882, 2734.425007, 2733.301013,
2732.930623, 2730.676264, 2729.172881, 2728.424855, 2727.298218,
2726.930066, 2726.177678, 2724.680797, 2723.5587, 2723.931652,
2722.428675, 2721.683421, 2720.557108, 2718.311893, 2716.438883,
2717.549484, 2717.545139, 2718.671354, 2714.548217, 2716.42254,
2713.060416, 2714.557005, 2714.554119, 2712.681174, 2711.561898,
2710.437904, 2710.438585, 2709.688272, 2707.067122, 2708.192203,
2705.564778, 2707.439879, 2705.949079, 2705.573079, 2705.577036,
2704.828393, 2702.954102, 2702.20199, 2701.079406, 2699.206235,
2697.708138, 2695.836101, 2696.588814, 2696.968721, 2696.966354,
2696.970764, 2696.22504, 2694.349549, 2695.852121)), .Names = c("Time",
"Fz"), row.names = 1299:1498, class = "data.frame")
Here's a possible approach using the zoo package
Function
library(zoo)
IntrvalsDif <- function(x, n) {
temp <- rollsum(x, n)
sapply(seq_len(length(x) - n + 1), function(x) temp[x + n] - temp[x])
}
Test
IntrvalsDif(df$Fz, 20)
# [1] 22.712120 31.780260 37.846728 48.042024 55.623904 59.831923 61.789132 67.470180 73.906584 79.581470
# [11] 81.889676 83.816224 80.498591 81.303658 85.113723 87.410109 91.579034 93.126436 97.284240 99.556696
# [21] 101.445531 99.204143 98.458872 96.574869 97.299629 101.391636 108.484213 111.843855 111.830721 108.817601
# [31] 108.804484 104.667484 103.899693 100.132011 94.484088 91.846433 88.453795 88.053184 85.777665 83.127912
# [41] 83.855688 84.967116 86.079728 84.951745 81.943294 79.311783 74.809550 70.305436 67.663273 68.031700
# [51] 68.010152 72.486929 78.453469 83.671546 86.262716 87.723876 86.573048 83.175009 80.529002 79.394259
# [61] 73.758934 70.745457 66.612867 64.351423 62.484932 55.371569 46.756346 42.618145 38.493014 34.730700
# [71] 27.612391 18.995970 6.271506 -3.829068 -10.178434 -18.028571 -24.761686 -32.615260 -40.093775 -46.838338
# [81] -52.458680 -61.082382 -69.714532 -78.721904 -86.991107 -88.898297 -88.564729 -94.941400 -102.449946 -114.087060
# [91] -120.115474 -131.764148 -140.435989 -151.731834 -164.906638 -176.581499 -185.261108 -191.700139 -198.144375 -208.693815
# [101] -217.378887 -226.065596 -232.122351 -241.185464 -251.002620 -264.177813 -277.722667 -288.293768 -297.354158 -303.775595
# [111] -313.566873 -314.745817 -316.279034 -317.070287 -318.979261 -322.381222 -328.406668 -334.420050 -342.305322 -345.705694
# [121] -351.343224 -352.849444 -359.231092 -360.347288 -361.457419 -363.321285 -366.306745 -366.670795 -366.308869 -364.077356
# [131] -365.210203 -366.711412 -371.584748 -371.950517 -369.317205 -366.687770 -363.297645 -361.044891 -356.536870 -352.777977
# [141] -349.395261 -347.887047 -344.504089 -340.758605 -337.002095 -329.879165 -319.755890 -313.731643 -310.687084 -311.395920
# [151] -304.987941 -305.325773 -300.807844 -303.409489 -306.756032 -306.724804 -305.584028 -302.559006 -301.033458 -301.379656
# [161] -296.103079 NA NA NA NA NA NA NA NA NA
# [171] NA NA NA NA NA NA NA NA NA NA
# [181] NA

Resources