R - difference between sums of consecutive pairs - r
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
Related
Split a sequence of numbers into groups of 10 digits using R
I would like for R to read in the first 10,000 digits of Pi and group every 10 digits together e.g., I want R to read in a sequence pi <- 3.14159265358979323846264338327950288419716939937510582097... and would like R to give me a table where each row contains 10 digit: 3141592653 5897932384 6264338327 ... I am new to R and really don't know where to start so any help would be much appreciated! Thank you in advance
https://rextester.com/OQRM27791 p <- strsplit("314159265358979323846264338327950288419716939937510582097", "") digits <- p[[1]] split(digits, ceiling((1:length(digits)) / 10));
Here's one way to do it. It's fully reproducible, so just cut and paste it into your R console. The vector result is the first 10,000 digits of pi, split into 1000 strings of 10 digits. For this many digits, I have used an online source for the precalculated value of pi. This is read in using readChar and the decimal point is stripped out with gsub. The resulting string is split into individual characters and put in a 1000 * 10 matrix (filled row-wise). The rows are then pasted into strings, giving the result. I have displayed only the first 100 entries of result for clarity of presentation. pi_url <- "https://www.pi2e.ch/blog/wp-content/uploads/2017/03/pi_dec_1m.txt" pi_char <- gsub("\\.", "", readChar(url, 1e4 + 1)) pi_mat <- matrix(strsplit(pi_char, "")[[1]], byrow = TRUE, ncol = 10) result <- apply(pi_mat, 1, paste0, collapse = "") head(result, 100) #> [1] "3141592653" "5897932384" "6264338327" "9502884197" "1693993751" #> [6] "0582097494" "4592307816" "4062862089" "9862803482" "5342117067" #> [11] "9821480865" "1328230664" "7093844609" "5505822317" "2535940812" #> [16] "8481117450" "2841027019" "3852110555" "9644622948" "9549303819" #> [21] "6442881097" "5665933446" "1284756482" "3378678316" "5271201909" #> [26] "1456485669" "2346034861" "0454326648" "2133936072" "6024914127" #> [31] "3724587006" "6063155881" "7488152092" "0962829254" "0917153643" #> [36] "6789259036" "0011330530" "5488204665" "2138414695" "1941511609" #> [41] "4330572703" "6575959195" "3092186117" "3819326117" "9310511854" #> [46] "8074462379" "9627495673" "5188575272" "4891227938" "1830119491" #> [51] "2983367336" "2440656643" "0860213949" "4639522473" "7190702179" #> [56] "8609437027" "7053921717" "6293176752" "3846748184" "6766940513" #> [61] "2000568127" "1452635608" "2778577134" "2757789609" "1736371787" #> [66] "2146844090" "1224953430" "1465495853" "7105079227" "9689258923" #> [71] "5420199561" "1212902196" "0864034418" "1598136297" "7477130996" #> [76] "0518707211" "3499999983" "7297804995" "1059731732" "8160963185" #> [81] "9502445945" "5346908302" "6425223082" "5334468503" "5261931188" #> [86] "1710100031" "3783875288" "6587533208" "3814206171" "7766914730" #> [91] "3598253490" "4287554687" "3115956286" "3882353787" "5937519577" #> [96] "8185778053" "2171226806" "6130019278" "7661119590" "9216420198" Created on 2020-07-23 by the reprex package (v0.3.0)
We can use str_extract: pi <- readLines("https://www.pi2e.ch/blog/wp-content/uploads/2017/03/pi_dec_1m.txt") library(stringr) t <- unlist(str_extract_all(sub("\\.","", pi), "\\d{10}")) t[1:100] [1] "3141592653" "5897932384" "6264338327" "9502884197" "1693993751" "0582097494" "4592307816" "4062862089" [9] "9862803482" "5342117067" "9821480865" "1328230664" "7093844609" "5505822317" "2535940812" "8481117450" [17] "2841027019" "3852110555" "9644622948" "9549303819" "6442881097" "5665933446" "1284756482" "3378678316" [25] "5271201909" "1456485669" "2346034861" "0454326648" "2133936072" "6024914127" "3724587006" "6063155881" [33] "7488152092" "0962829254" "0917153643" "6789259036" "0011330530" "5488204665" "2138414695" "1941511609" [41] "4330572703" "6575959195" "3092186117" "3819326117" "9310511854" "8074462379" "9627495673" "5188575272" [49] "4891227938" "1830119491" "2983367336" "2440656643" "0860213949" "4639522473" "7190702179" "8609437027" [57] "7053921717" "6293176752" "3846748184" "6766940513" "2000568127" "1452635608" "2778577134" "2757789609" [65] "1736371787" "2146844090" "1224953430" "1465495853" "7105079227" "9689258923" "5420199561" "1212902196" [73] "0864034418" "1598136297" "7477130996" "0518707211" "3499999983" "7297804995" "1059731732" "8160963185" [81] "9502445945" "5346908302" "6425223082" "5334468503" "5261931188" "1710100031" "3783875288" "6587533208" [89] "3814206171" "7766914730" "3598253490" "4287554687" "3115956286" "3882353787" "5937519577" "8185778053" [97] "2171226806" "6130019278" "7661119590" "9216420198"
R: Convert time series xyz to raster
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
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
Dividing components of a vector into several data points in R
I am trying to turn a vector of length n (say, 14), and turn it into a vector of length N (say, 90). For example, my vector is x<-c(5,3,7,11,12,19,40,2,22,6,10,12,12,4) and I want to turn it into a vector of length 90, by creating 90 equally "spaced" points on this vector- think of x as a function. Is there any way to do that in R?
Something like this? > x<-c(5,3,7,11,12,19,40,2,22,6,10,12,12,4) > seq(min(x),max(x),length=90) [1] 2.000000 2.426966 2.853933 3.280899 3.707865 4.134831 4.561798 [8] 4.988764 5.415730 5.842697 6.269663 6.696629 7.123596 7.550562 [15] 7.977528 8.404494 8.831461 9.258427 9.685393 10.112360 10.539326 [22] 10.966292 11.393258 11.820225 12.247191 12.674157 13.101124 13.528090 [29] 13.955056 14.382022 14.808989 15.235955 15.662921 16.089888 16.516854 [36] 16.943820 17.370787 17.797753 18.224719 18.651685 19.078652 19.505618 [43] 19.932584 20.359551 20.786517 21.213483 21.640449 22.067416 22.494382 [50] 22.921348 23.348315 23.775281 24.202247 24.629213 25.056180 25.483146 [57] 25.910112 26.337079 26.764045 27.191011 27.617978 28.044944 28.471910 [64] 28.898876 29.325843 29.752809 30.179775 30.606742 31.033708 31.460674 [71] 31.887640 32.314607 32.741573 33.168539 33.595506 34.022472 34.449438 [78] 34.876404 35.303371 35.730337 36.157303 36.584270 37.011236 37.438202 [85] 37.865169 38.292135 38.719101 39.146067 39.573034 40.000000 >
Try this: #data x <- c(5,3,7,11,12,19,40,2,22,6,10,12,12,4) #expected new length N=90 #number of numbers between 2 numbers my.length.out=round((N-length(x))/(length(x)-1))+1 #new data x1 <- unlist( lapply(1:(length(x)-1), function(i) seq(x[i],x[i+1],length.out = my.length.out))) #plot par(mfrow=c(2,1)) plot(x) plot(x1)
R: Using for loop on data frame
I have a data frame, deflator. I want to get a new data frame inflation which can be calculated by: deflator[i] - deflator[i-4] ----------------------------- * 100 deflator [i - 4] The data frame deflator has 71 numbers: > deflator [1] 0.9628929 0.9596746 0.9747274 0.9832532 0.9851884 [6] 0.9797770 0.9913502 1.0100561 1.0176906 1.0092516 [11] 1.0185932 1.0241043 1.0197975 1.0174097 1.0297328 [16] 1.0297071 1.0313232 1.0244618 1.0347808 1.0480411 [21] 1.0322142 1.0351968 1.0403264 1.0447121 1.0504402 [26] 1.0487097 1.0664664 1.0935239 1.0965951 1.1141851 [31] 1.1033155 1.1234482 1.1333870 1.1188136 1.1336276 [36] 1.1096461 1.1226584 1.1287245 1.1529588 1.1582911 [41] 1.1691221 1.1782178 1.1946234 1.1963453 1.1939922 [46] 1.2118189 1.2227960 1.2140535 1.2228828 1.2314258 [51] 1.2570788 1.2572214 1.2607763 1.2744415 1.2982076 [56] 1.3318808 1.3394186 1.3525902 1.3352815 1.3492751 [61] 1.3593859 1.3368135 1.3642940 1.3538567 1.3658135 [66] 1.3710932 1.3888638 1.4262185 1.4309707 1.4328823 [71] 1.4497201 This is a very tricky question for me. I tried to do this using a for loop: > d <- data.frame(deflator) > for (i in 1:71) {d <-rbind(d,c(delfaotr ))} I think I might be doing it wrong.
Why use data frames? This is a straightforward vector operation. inflation = 100 * (deflator[1:67] - deflator[-(1:4)])/deflator[-(1:4)]
I agree with #Fhnuzoag that your example suggests calculations on a numeric vector, not a data frame. Here's an additional way to do your calculations taking advantage of the lag argument in the diff function (with indexes that match those in your question): lagBy <- 4 # The number of indexes by which to lag laggedDiff <- diff(deflator, lag = lagBy) # The numerator above theDenom <- deflator[seq_len(length(deflator) - lagBy)] # The denominator above inflation <- laggedDiff/theDenom The first few results are: head(inflation) # [1] 0.02315470 0.02094710 0.01705379 0.02725941 0.03299085 0.03008297