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