Change range of "fill" values in choropleth map R - r

Any help would be greatly appreciated!!
I'm trying to create a choropleth map in R that shows the counties of texas, color-coded by their population ranges.
My problem is that the range of populations is too large. The highest population is over 4 million, but most of the counties have a population under 50,000. The criteria for the fill is: (0-1mil), (1-2mil), (2-3mil), (3-4mil), (4-5mil) but almost all fall under 0-1mil.
How can I change the legend to account for different ranges of numbers? For example, maybe:
(0-1,000), (1,000-10,000), (10,000-100,000), (100,000-1mil), (1mil-5mil)
Here's the code I wrote to plot the data:
txplot <- ggplot(txczpop, aes(fill=pop2014)) + geom_map(txmap)
tm_shape(txmap) +
tm_fill("pop2014", title="TX County Population", palette = "PRGn") +
tm_borders(alpha=.5) +
tm_style_beaver()
Here's the result:
[![enter image description here][1]][1]
I'm using a census county shapefile and population also retrieved from a census file.
Here's the output of my population data:
txczpop <- structure(list(county_fips = c(48001L, 48003L, 48005L, 48007L,
48009L, 48011L, 48013L, 48015L, 48017L, 48019L, 48021L, 48023L,
48025L, 48027L, 48029L, 48031L, 48033L, 48035L, 48037L, 48039L,
48041L, 48043L, 48045L, 48047L, 48049L, 48051L, 48053L, 48055L,
48057L, 48059L, 48061L, 48063L, 48065L, 48067L, 48069L, 48071L,
48073L, 48075L, 48077L, 48079L, 48081L, 48083L, 48085L, 48087L,
48089L, 48091L, 48093L, 48095L, 48097L, 48099L, 48101L, 48103L,
48105L, 48107L, 48109L, 48111L, 48113L, 48115L, 48117L, 48119L,
48121L, 48123L, 48125L, 48127L, 48129L, 48131L, 48133L, 48135L,
48137L, 48141L, 48139L, 48143L, 48145L, 48147L, 48149L, 48151L,
48153L, 48155L, 48157L, 48159L, 48161L, 48163L, 48165L, 48167L,
48169L, 48171L, 48173L, 48175L, 48177L, 48179L, 48181L, 48183L,
48185L, 48187L, 48189L, 48191L, 48193L, 48195L, 48197L, 48199L,
48201L, 48203L, 48205L, 48207L, 48209L, 48211L, 48213L, 48215L,
48217L, 48219L, 48221L, 48223L, 48225L, 48227L, 48229L, 48231L,
48233L, 48235L, 48237L, 48239L, 48241L, 48243L, 48245L, 48247L,
48249L, 48251L, 48253L, 48255L, 48257L, 48259L, 48261L, 48263L,
48265L, 48267L, 48269L, 48271L, 48273L, 48275L, 48283L, 48277L,
48279L, 48281L, 48285L, 48287L, 48289L, 48291L, 48293L, 48295L,
48297L, 48299L, 48301L, 48303L, 48305L, 48313L, 48315L, 48317L,
48319L, 48321L, 48323L, 48307L, 48309L, 48311L, 48325L, 48327L,
48329L, 48331L, 48333L, 48335L, 48337L, 48339L, 48341L, 48343L,
48345L, 48347L, 48349L, 48351L, 48353L, 48355L, 48357L, 48359L,
48361L, 48363L, 48365L, 48367L, 48369L, 48371L, 48373L, 48375L,
48377L, 48379L, 48381L, 48383L, 48385L, 48387L, 48389L, 48391L,
48393L, 48395L, 48397L, 48399L, 48401L, 48403L, 48405L, 48407L,
48409L, 48411L, 48413L, 48415L, 48417L, 48419L, 48421L, 48423L,
48425L, 48427L, 48429L, 48431L, 48433L, 48435L, 48437L, 48439L,
48441L, 48443L, 48445L, 48447L, 48449L, 48451L, 48453L, 48455L,
48457L, 48459L, 48461L, 48463L, 48465L, 48467L, 48469L, 48471L,
48473L, 48475L, 48477L, 48479L, 48481L, 48483L, 48485L, 48487L,
48489L, 48491L, 48493L, 48495L, 48497L, 48499L, 48501L, 48503L,
48505L, 48507L), county_name = c("Anderson", "Andrews", "Angelina",
"Aransas", "Archer", "Armstrong", "Atascosa", "Austin", "Bailey",
"Bandera", "Bastrop", "Baylor", "Bee", "Bell", "Bexar", "Blanco",
"Borden", "Bosque", "Bowie", "Brazoria", "Brazos", "Brewster",
"Briscoe", "Brooks", "Brown", "Burleson", "Burnet", "Caldwell",
"Calhoun", "Callahan", "Cameron", "Camp", "Carson", "Cass", "Castro",
"Chambers", "Cherokee", "Childress", "Clay", "Cochran", "Coke",
"Coleman", "Collin", "Collingsworth", "Colorado", "Comal", "Comanche",
"Concho", "Cooke", "Coryell", "Cottle", "Crane", "Crockett",
"Crosby", "Culberson", "Dallam", "Dallas", "Dawson", "Deaf Smith",
"Delta", "Denton", "DeWitt", "Dickens", "Dimmit", "Donley", "Duval",
"Eastland", "Ector", "Edwards", "El Paso", "Ellis", "Erath",
"Falls", "Fannin", "Fayette", "Fisher", "Floyd", "Foard", "Fort Bend",
"Franklin", "Freestone", "Frio", "Gaines", "Galveston", "Garza",
"Gillespie", "Glasscock", "Goliad", "Gonzales", "Gray", "Grayson",
"Gregg", "Grimes", "Guadalupe", "Hale", "Hall", "Hamilton", "Hansford",
"Hardeman", "Hardin", "Harris", "Harrison", "Hartley", "Haskell",
"Hays", "Hemphill", "Henderson", "Hidalgo", "Hill", "Hockley",
"Hood", "Hopkins", "Houston", "Howard", "Hudspeth", "Hunt", "Hutchinson",
"Irion", "Jack", "Jackson", "Jasper", "Jeff Davis", "Jefferson",
"Jim Hogg", "Jim Wells", "Johnson", "Jones", "Karnes", "Kaufman",
"Kendall", "Kenedy", "Kent", "Kerr", "Kimble", "King", "Kinney",
"Kleberg", "Knox", "La Salle", "Lamar", "Lamb", "Lampasas", "Lavaca",
"Lee", "Leon", "Liberty", "Limestone", "Lipscomb", "Live Oak",
"Llano", "Loving", "Lubbock", "Lynn", "Madison", "Marion", "Martin",
"Mason", "Matagorda", "Maverick", "McCulloch", "McLennan", "McMullen",
"Medina", "Menard", "Midland", "Milam", "Mills", "Mitchell",
"Montague", "Montgomery", "Moore", "Morris", "Motley", "Nacogdoches",
"Navarro", "Newton", "Nolan", "Nueces", "Ochiltree", "Oldham",
"Orange", "Palo Pinto", "Panola", "Parker", "Parmer", "Pecos",
"Polk", "Potter", "Presidio", "Rains", "Randall", "Reagan", "Real",
"Red River", "Reeves", "Refugio", "Roberts", "Robertson", "Rockwall",
"Runnels", "Rusk", "Sabine", "San Augustine", "San Jacinto",
"San Patricio", "San Saba", "Schleicher", "Scurry", "Shackelford",
"Shelby", "Sherman", "Smith", "Somervell", "Starr", "Stephens",
"Sterling", "Stonewall", "Sutton", "Swisher", "Tarrant", "Taylor",
"Terrell", "Terry", "Throckmorton", "Titus", "Tom Green", "Travis",
"Trinity", "Tyler", "Upshur", "Upton", "Uvalde", "Val Verde",
"Van Zandt", "Victoria", "Walker", "Waller", "Ward", "Washington",
"Webb", "Wharton", "Wheeler", "Wichita", "Wilbarger", "Willacy",
"Williamson", "Wilson", "Winkler", "Wise", "Wood", "Yoakum",
"Young", "Zapata", "Zavala"), pop2014 = c(57627L, 17477L, 87750L,
24972L, 8811L, 1955L, 47774L, 29114L, 6910L, 20892L, 78069L,
3592L, 32863L, 329140L, 1855866L, 10812L, 652L, 17780L, 93275L,
338124L, 209152L, 9173L, 1536L, 7194L, 37653L, 17253L, 44943L,
39810L, 21797L, 13513L, 420392L, 12621L, 6013L, 30261L, 7781L,
38145L, 50902L, 7089L, 10370L, 2935L, 3254L, 8430L, 885241L,
3017L, 20719L, 123694L, 13550L, 4050L, 38761L, 75562L, 1415L,
4950L, 3812L, 5899L, 2266L, 7135L, 2518638L, 13372L, 19195L,
5238L, 753363L, 20684L, 2218L, 11089L, 3543L, 11533L, 18176L,
153904L, 1879L, 833487L, 159317L, 40147L, 16989L, 33752L, 24833L,
3831L, 5949L, 1275L, 685345L, 10600L, 19762L, 18531L, 19425L,
314198L, 6435L, 25520L, 1291L, 7549L, 20462L, 23044L, 123534L,
123204L, 27172L, 147250L, 34720L, 3147L, 8199L, 5509L, 3928L,
55621L, 4441370L, 67336L, 6089L, 5769L, 185025L, 4180L, 79290L,
831073L, 34848L, 23577L, 53921L, 35921L, 22741L, 36651L, 3211L,
88493L, 21773L, 1574L, 8855L, 14739L, 35552L, 2204L, 252235L,
5255L, 41353L, 157456L, 19936L, 14906L, 111236L, 38880L, 400L,
785L, 50562L, 4438L, 262L, 3526L, 32190L, 3858L, 7474L, 49523L,
13574L, 20156L, 19721L, 16742L, 16861L, 78117L, 23524L, 3553L,
12091L, 19510L, 86L, 293974L, 5771L, 13861L, 10149L, 5460L, 4071L,
36519L, 57023L, 8199L, 243441L, 805L, 47894L, 2147L, 155830L,
24256L, 4870L, 9076L, 19416L, 518947L, 22148L, 12743L, 1153L,
65301L, 48195L, 14138L, 15093L, 356221L, 10758L, 2070L, 83433L,
28096L, 23769L, 123164L, 9908L, 15893L, 46079L, 121627L, 6976L,
11032L, 128220L, 3755L, 3371L, 12446L, 14349L, 7302L, 928L, 16500L,
87809L, 10416L, 53923L, 10350L, 8610L, 27099L, 66915L, 5622L,
3162L, 17328L, 3343L, 25515L, 3084L, 218842L, 8694L, 62955L,
9405L, 1339L, 1403L, 3972L, 7581L, 1945360L, 135143L, 927L, 12739L,
1608L, 32506L, 116608L, 1151145L, 14224L, 21418L, 40354L, 3454L,
27117L, 48974L, 52910L, 91081L, 69789L, 46820L, 11625L, 34438L,
266673L, 41168L, 5714L, 132355L, 12973L, 21903L, 489250L, 46402L,
7821L, 61638L, 42852L, 8286L, 18350L, 14319L, 12267L)), .Names = c("county_fips",
"county_name", "pop2014"), row.names = c(5100L, 5101L, 5103L,
5106L, 5107L, 5109L, 5112L, 5114L, 5116L, 5118L, 5120L, 5121L,
5124L, 5126L, 5128L, 5129L, 5131L, 5133L, 5136L, 5137L, 5140L,
5141L, 5143L, 5146L, 5147L, 5150L, 5152L, 5153L, 5156L, 5158L,
5159L, 5161L, 5163L, 5166L, 5168L, 5170L, 5171L, 5174L, 5176L,
5178L, 5179L, 5182L, 5183L, 5185L, 5188L, 5190L, 5192L, 5194L,
5195L, 5198L, 5200L, 5201L, 5203L, 5205L, 5208L, 5209L, 5212L,
5214L, 5215L, 5218L, 5219L, 5221L, 5224L, 5226L, 5228L, 5230L,
5232L, 5233L, 5235L, 5239L, 5237L, 5242L, 5244L, 5245L, 5248L,
5249L, 5251L, 5254L, 5256L, 5257L, 5260L, 5261L, 5264L, 5265L,
5268L, 5270L, 5272L, 5274L, 5276L, 5278L, 5280L, 5281L, 5284L,
5286L, 5288L, 5290L, 5292L, 5293L, 5296L, 5298L, 5300L, 5301L,
5303L, 5306L, 5308L, 5309L, 5312L, 5314L, 5316L, 5317L, 5319L,
5321L, 5323L, 5326L, 5327L, 5330L, 5332L, 5334L, 5335L, 5337L,
5339L, 5341L, 5343L, 5346L, 5348L, 5349L, 5352L, 5354L, 5356L,
5357L, 5360L, 5362L, 5364L, 5365L, 5368L, 5369L, 5372L, 5374L,
5382L, 5376L, 5378L, 5379L, 5383L, 5385L, 5388L, 5390L, 5392L,
5394L, 5396L, 5398L, 5400L, 5401L, 5404L, 5412L, 5413L, 5416L,
5418L, 5419L, 5421L, 5406L, 5407L, 5409L, 5423L, 5425L, 5427L,
5429L, 5432L, 5434L, 5435L, 5438L, 5440L, 5442L, 5443L, 5446L,
5448L, 5449L, 5451L, 5453L, 5456L, 5457L, 5460L, 5461L, 5464L,
5465L, 5468L, 5470L, 5472L, 5474L, 5476L, 5477L, 5480L, 5482L,
5484L, 5486L, 5488L, 5489L, 5491L, 5494L, 5496L, 5498L, 5499L,
5501L, 5504L, 5505L, 5508L, 5510L, 5511L, 5514L, 5516L, 5518L,
5520L, 5522L, 5524L, 5526L, 5527L, 5530L, 5531L, 5533L, 5536L,
5537L, 5540L, 5542L, 5544L, 5546L, 5547L, 5550L, 5552L, 5554L,
5555L, 5558L, 5559L, 5562L, 5563L, 5566L, 5568L, 5569L, 5571L,
5574L, 5575L, 5578L, 5579L, 5582L, 5584L, 5585L, 5587L, 5590L,
5592L, 5594L, 5595L, 5598L, 5600L, 5602L, 5604L, 5606L), class = "data.frame")

I just created a new column in the population dataframe that summarizes the population based on the ranges that I want to use, and then use that as the criteria for the fill:
txczpop$poprange[txczpop$pop2014 >= 0 & txczpop < 1000] <- "0-1,000"
txczpop$poprange[txczpop$pop2014 >= 1000 & txczpop < 10000] <- "1-10,000"
txczpop$poprange[txczpop$pop2014 >= 10000 & txczpop$pop2014 < 100000] <- "10,000-100,000"
txczpop$poprange[txczpop$pop2014 >= 100000 & txczpop$pop2014 < 1000000] <- "100,000 - 1,000,000"
txczpop$poprange[txczpop$pop2014 >= 1000000 & txczpop$pop2014 <= 5000000] <- "1,000,000 - 5,000,000"

Related

Updating dataframe column value by referring to another dataframe

My first data frame (df) contains Entrydate and ExitDate columns. Another dataframe (n1) has all trading dates. I need a new column in first dataframe calculated as number of days as calculated from the second dataframe. How do I call this dayCount function for each row of df. When I try to use mapply, I am unable to pass n1 as a parameter.
dayCount <- function (startDate, endDate, n1) {
return (nrow(subset(n1, Date >= startDate & Date <= endDate)))
}
df<- structure(list(EntryDate = structure(c(11355, 11418, 11436, 11449,
11520, 11523, 11548, 11620, 11768, 11773), class = "Date"), ExitDate = structure(c(11360,
11422, 11438, 11457, 11522, 11526, 11554, 11625, 11772, 11778
), class = "Date")), row.names = c(22L, 65L, 76L, 84L, 135L,
138L, 155L, 204L, 305L, 307L), class = "data.frame")
n1<- structure(c(11354, 11355, 11358, 11359, 11360, 11361, 11362,
11365, 11366, 11367, 11368, 11369, 11372, 11373, 11374, 11375,
11376, 11379, 11380, 11381, 11382, 11383, 11386, 11388, 11389,
11390, 11393, 11394, 11395, 11396, 11397, 11400, 11401, 11402,
11403, 11404, 11407, 11408, 11409, 11410, 11411, 11414, 11415,
11416, 11418, 11421, 11422, 11423, 11424, 11428, 11429, 11430,
11431, 11432, 11435, 11436, 11437, 11438, 11439, 11442, 11444,
11445, 11446, 11449, 11450, 11451, 11452, 11453, 11456, 11457,
11458, 11459, 11460, 11463, 11464, 11465, 11466, 11467, 11470,
11471, 11472, 11473, 11474, 11477, 11478, 11479, 11480, 11481,
11484, 11485, 11486, 11487, 11488, 11491, 11492, 11493, 11494,
11495, 11498, 11499, 11500, 11501, 11502, 11505, 11506, 11507,
11508, 11509, 11512, 11513, 11514, 11515, 11516, 11519, 11520,
11521, 11522, 11523, 11526, 11527, 11528, 11529, 11530, 11533,
11534, 11535, 11536, 11537, 11540, 11541, 11542, 11543, 11544,
11547, 11548, 11550, 11551, 11554, 11555, 11557, 11558, 11561,
11562, 11563, 11564, 11565, 11568, 11569, 11570, 11571, 11572,
11575, 11576, 11577, 11578, 11579, 11582, 11583, 11584, 11585,
11586, 11589, 11590, 11591, 11592, 11593, 11596, 11598, 11599,
11600, 11603, 11604, 11605, 11606, 11607, 11610, 11611, 11612,
11613, 11614, 11617, 11618, 11619, 11620, 11624, 11625, 11626,
11627, 11628, 11631, 11632, 11633, 11634, 11635, 11638, 11639,
11640, 11641, 11645, 11646, 11647, 11648, 11649, 11652, 11653,
11654, 11655, 11659, 11660, 11661, 11662, 11663, 11666, 11667,
11668, 11669, 11670, 11674, 11675, 11676, 11677, 11680, 11682,
11683, 11684, 11687, 11688, 11689, 11690, 11691, 11694, 11695,
11696, 11697, 11698, 11701, 11702, 11703, 11704, 11705, 11708,
11709, 11710, 11711, 11712, 11715, 11716, 11717, 11718, 11719,
11722, 11723, 11724, 11725, 11726, 11729, 11730, 11731, 11732,
11733, 11736, 11737, 11738, 11739, 11740, 11743, 11744, 11745,
11746, 11747, 11750, 11751, 11752, 11753, 11754, 11757, 11758,
11759, 11760, 11761, 11764, 11765, 11766, 11767, 11768, 11772,
11773, 11774, 11778), class = "Date")
You can use %in% to count number of days in n1 between each EntryDate and ExitDate.
df$dayCount <- colSums(mapply(function(x, y) n1 %in% seq(x, y, by = '1 day'),
df$EntryDate, df$ExitDate))
df
# EntryDate ExitDate dayCount
#22 2001-02-02 2001-02-07 4
#65 2001-04-06 2001-04-10 3
#76 2001-04-24 2001-04-26 3
#84 2001-05-07 2001-05-15 7
#135 2001-07-17 2001-07-19 3
#138 2001-07-20 2001-07-23 2
#155 2001-08-14 2001-08-20 4
#204 2001-10-25 2001-10-30 3
#305 2002-03-22 2002-03-26 2
#307 2002-03-27 2002-04-01 3

Finding increases from 'baseline' in the graph, not sure how to do

I want to write an algorithm that spits out the points highlighted by arrows. I've tried using a second derivative but it returns a similar plot to the one above and not sure how to use it.
Hi, sorry about that, I don't want the peaks, I want the point where the graph starts to increase - ie I want the point where the gradient changes from ~0 to something larger, does that make sense
Example data is below.
df = structure(list(X1 = c("2729", "2730", "2731", "2732", "2733",
"2734", "2735", "2736", "2737", "2738", "2739", "2740", "2741",
"2742", "2743", "2744", "2745", "2746", "2747", "2748", "2749",
"2750", "2751", "2752", "2753", "2754", "2755", "2756", "2757",
"2758", "2759", "2760", "2761", "2762", "2763", "2764", "2765",
"2766", "2767", "2768", "2769", "2770", "2771", "2772", "2773",
"2774", "2775", "2776", "2777", "2778", "2779", "2780", "2781",
"2782", "2783", "2784", "2785", "2786", "2787", "2788", "2789",
"2790", "2791", "2792", "2793", "2794", "2795", "2796", "2797",
"2798", "2799", "2800", "2801", "2802", "2803", "2804", "2805",
"2806", "2807", "2808", "2809", "2810", "2811", "2812", "2813",
"2814", "2815", "2816", "2817", "2818", "2819", "2820", "2821",
"2822", "2823", "2824", "2825", "2826", "2827", "2828", "2829",
"2830", "2831", "2832", "2833", "2834", "2835", "2836", "2837",
"2838", "2839", "2840", "2841", "2842", "2843", "2844", "2845",
"2846", "2847", "2848", "2849", "2850", "2851", "2852", "2853",
"2854", "2855", "2856", "2857", "2858", "2859", "2860", "2861",
"2862", "2863", "2864", "2865", "2866", "2867", "2868", "2869",
"2870", "2871", "2872", "2873", "2874", "2875", "2876", "2877",
"2878", "2879", "2880", "2881", "2882", "2883", "2884", "2885",
"2886", "2887", "2888", "2889", "2890", "2891", "2892", "2893",
"2894", "2895", "2896", "2897", "2898", "2899", "2900", "2901",
"2902", "2903", "2904", "2905", "2906", "2907", "2908", "2909",
"2910", "2911", "2912", "2913", "2914", "2915", "2916", "2917",
"2918", "2919", "2920", "2921", "2922", "2923", "2924", "2925",
"2926", "2927", "2928", "2929", "2930", "2931", "2932", "2933",
"2934", "2935", "2936", "2937", "2938", "2939", "2940", "2941",
"2942", "2943", "2944", "2945", "2946", "2947", "2948", "2949",
"2950", "2951", "2952", "2953", "2954", "2955", "2956", "2957",
"2958", "2959", "2960", "2961", "2962", "2963", "2964", "2965",
"2966", "2967", "2968", "2969", "2970", "2971", "2972", "2973",
"2974", "2975", "2976", "2977", "2978", "2979", "2980", "2981",
"2982", "2983", "2984", "2985", "2986", "2987", "2988", "2989",
"2990", "2991", "2992", "2993", "2994", "2995", "2996", "2997",
"2998", "2999", "3000", "3001", "3002", "3003", "3004", "3005",
"3006", "3007", "3008", "3009", "3010", "3011", "3012", "3013",
"3014", "3015", "3016", "3017", "3018", "3019", "3020", "3021",
"3022", "3023", "3024", "3025", "3026", "3027", "3028", "3029",
"3030", "3031", "3032", "3033", "3034", "3035", "3036", "3037",
"3038", "3039", "3040", "3041", "3042", "3043", "3044", "3045",
"3046", "3047", "3048", "3049", "3050", "3051", "3052", "3053",
"3054", "3055", "3056", "3057", "3058", "3059", "3060", "3061",
"3062", "3063", "3064", "3065", "3066", "3067", "3068", "3069",
"3070", "3071", "3072", "3073", "3074", "3075", "3076", "3077",
"3078", "3079", "3080", "3081", "3082", "3083", "3084", "3085",
"3086", "3087", "3088", "3089", "3090", "3091", "3092", "3093",
"3094", "3095", "3096", "3097", "3098", "3099", "3100", "3101",
"3102", "3103", "3104", "3105", "3106", "3107", "3108", "3109",
"3110", "3111", "3112", "3113", "3114", "3115", "3116", "3117",
"3118", "3119", "3120", "3121", "3122", "3123", "3124", "3125",
"3126", "3127", "3128", "3129", "3130", "3131", "3132", "3133",
"3134", "3135", "3136", "3137", "3138", "3139", "3140", "3141",
"3142", "3143", "3144", "3145", "3146", "3147", "3148", "3149",
"3150", "3151", "3152", "3153", "3154", "3155", "3156", "3157",
"3158", "3159", "3160", "3161", "3162", "3163", "3164", "3165",
"3166", "3167", "3168", "3169", "3170", "3171", "3172", "3173",
"3174", "3175", "3176", "3177", "3178", "3179", "3180", "3181",
"3182", "3183", "3184", "3185", "3186", "3187", "3188", "3189",
"3190", "3191", "3192", "3193", "3194", "3195", "3196", "3197",
"3198", "3199", "3200", "3201", "3202", "3203", "3204", "3205",
"3206", "3207", "3208", "3209", "3210", "3211", "3212", "3213",
"3214", "3215", "3216", "3217", "3218", "3219", "3220", "3221",
"3222", "3223", "3224", "3225", "3226", "3227", "3228", "3229",
"3230", "3231", "3232", "3233", "3234", "3235", "3236", "3237",
"3238", "3239", "3240", "3241", "3242", "3243", "3244", "3245",
"3246", "3247", "3248", "3249", "3250", "3251", "3252", "3253",
"3254", "3255", "3256", "3257", "3258", "3259", "3260", "3261",
"3262", "3263", "3264", "3265", "3266", "3267", "3268", "3269",
"3270", "3271", "3272", "3273", "3274", "3275", "3276", "3277",
"3278", "3279", "3280", "3281", "3282", "3283", "3284", "3285",
"3286", "3287", "3288", "3289", "3290", "3291", "3292", "3293",
"3294", "3295", "3296", "3297", "3298", "3299", "3300", "3301",
"3302", "3303", "3304", "3305", "3306", "3307", "3308", "3309",
"3310", "3311", "3312", "3313", "3314", "3315", "3316", "3317",
"3318", "3319", "3320", "3321", "3322", "3323", "3324", "3325",
"3326", "3327", "3328", "3329", "3330", "3331", "3332", "3333",
"3334", "3335", "3336", "3337", "3338", "3339", "3340", "3341",
"3342", "3343", "3344", "3345", "3346", "3347", "3348", "3349",
"3350", "3351", "3352", "3353", "3354", "3355", "3356", "3357",
"3358", "3359", "3360", "3361", "3362", "3363", "3364", "3365",
"3366", "3367", "3368", "3369", "3370", "3371", "3372", "3373",
"3374", "3375", "3376", "3377", "3378", "3379", "3380", "3381",
"3382", "3383", "3384", "3385", "3386", "3387", "3388", "3389",
"3390", "3391", "3392", "3393", "3394", "3395", "3396", "3397",
"3398", "3399", "3400", "3401", "3402", "3403", "3404", "3405",
"3406", "3407", "3408", "3409", "3410", "3411", "3412", "3413",
"3414", "3415", "3416", "3417", "3418", "3419", "3420", "3421",
"3422", "3423", "3424", "3425", "3426", "3427", "3428", "3429",
"3430", "3431", "3432", "3433", "3434", "3435", "3436", "3437",
"3438", "3439", "3440", "3441", "3442", "3443", "3444", "3445"
), X2 = c(-0.00385000000001254, -0.0154500000000484, -0.0277600000000007,
-0.0154500000000279, -0.0386000000000704, -0.0154500000000329,
-0.0115500000000053, 2.5238009638656e-15, -0.00385000000000757,
3.60475000000867, -0.470850000000881, -0.347350000000663, -0.173700000000328,
-0.139699999999998, -0.096500000000187, -0.0617500000001111,
-0.0579000000001016, -0.0424500000000768, -0.050150000000105,
-0.0579000000001191, -0.0540000000000976, -0.0579000000001924,
-0.0270000000000563, -0.0309000000000539, -0.0231500000000468,
-0.0270500000000538, -0.00775000000002209, -0.0193000000000404,
-0.0131199999999931, 0.219999999999842, 0.0579000000001427, -0.061750000000126,
-0.0617500000002055, -0.0309000000000726, -0.050150000000105,
-0.042450000000091, -0.0193000000000293, -0.0309000000000144,
-0.0115500000000196, -0.0116000000000154, -0.0154500000000366,
-0.00385000000000946, -0.0193000000000305, -0.00390000000000946,
-0.00390000000000639, -0.00771000000000015, -0.000789999999999225,
-4.97400384373025e-15, -0.00619000000000085, -0.0116000000000265,
-0.011550000000014, -0.00385000000000504, -0.00538999999999987,
-0.0116000000000203, -0.011550000000014, 0.00385000000001136,
-0.00230999999999795, 2.86419210237446e-15, -0.00230999999999954,
-0.00770000000002508, -0.00770000000001703, -0.00390000000000449,
-0.0085000000000008, -0.0193000000000529, -8.05101707233625e-15,
-0.00385000000001751, -0.0146699999999988, -0.00619000000000085,
-0.0116000000000265, 0.00153999999999996, 0.00385000000000546,
-0.00231000000000233, -0.000780000000000314, -0.00230999999999884,
0.0015400000000021, -8.05101707233625e-15, -0.00848000000000013,
-0.00385000000001751, -0.00775000000003729, -0.00769999999999792,
-1.1787959787484e-15, -0.00384999999999692, 0.00385000000001136,
-0.00384999999999762, 0.00385000000000639, -0.00385000000001161,
-0.000440000000001542, -0.00390000000000639, -0.000769999999999981,
0, -0.0154500000000091, -0.0077500000000059, -0.0154500000000335,
-0.0115500000000165, -0.00385000000000567, -0.00311000000000092,
0.0116000000000272, -0.00230999999999994, 0.0116000000000172,
0.00770000000001277, -0.00385000000000377, -0.00385000000001254,
0.00385000000001136, -0.00385000000000411, -0.0038499999999997,
-0.0116000000000215, -0.0154300000000006, -6.15348059644161e-15,
-0.00849999999999866, -0.0015500000000003, 0.00154000000000174,
-3.07674029821757e-15, -0.0115500000000345, -0.0115500000000165,
-6.15348059644161e-15, -0.00385000000002247, 0.0077000000000059,
-0.00385000000001254, -0.0115500000000315, -0.0154500000000107,
-0.0154500000000229, -0.0309000000000733, -1.65190000000256,
-0.258600000000477, -0.111900000000204, -0.0640499999999989,
-0.0579000000001016, -0.0270000000000494, -0.02393, -0.0193000000000324,
-0.0115500000000165, -0.0270000000000624, -0.0193000000000598,
-0.0309000000000733, -0.0463000000001036, -2.19220000000482,
-0.524900000000959, -0.189100000000636, -0.11580000000022, -0.0717700000000001,
-0.0424500000001407, -0.057900000000101, -0.0386000000000673,
-0.0193000000000449, -0.0277899999999995, -0.0077500000000276,
-0.0208600000000011, -0.0193000000000293, -0.0463000000000912,
-0.0386000000000716, -0.0501500000001031, -0.0347500000000728,
-0.0502000000000926, -0.0424500000000836, -0.00307999999999993,
-0.0116000000000234, 0.00389999999999833, -0.000769999999999981,
-0.00153999999999996, -0.00153999999999996, 0.00153999999999783,
-0.0162100000000009, -0.0386000000000797, -0.0432300000000026,
-0.038600000000117, -0.050200000000097, -0.0309000000000527,
-0.0231500000000593, 0.00461999999999989, -0.00385000000001064,
-0.00385000000000757, -0.0116000000000215, 0.00770000000004104,
0.00385000000000639, -0.941700000001459, -0.169850000000308,
-0.100350000000196, -0.0933799999999984, -0.0617500000001154,
-0.0579000000001165, -0.0386000000000822, -0.019300000000043,
-0.0231500000000629, -0.0115500000000165, -0.0270000000000464,
-0.0116000000000284, -0.00769999999999982, -2.76340000000441,
-0.270200000000513, -0.119650000000229, -0.108100000000387, -0.0540000000001033,
-0.0772000000001527, -0.0579000000001345, -0.0656000000001255,
-0.0540500000001704, -0.0386000000000716, -0.0270500000000663,
-0.0116000000000284, -0.0216200000000043, -0.00770000000001206,
-0.0308500000000552, -0.0115500000000265, -2.4190463576414e-14,
-0.00770000000003006, -0.0115900000000011, -0.0231500000000985,
-0.0193000000000293, -0.033979999999999, -0.00775000000002643,
-0.0478400000000022, -0.0231500000000412, -0.019300000000043,
-0.00233000000000134, -0.00390000000002501, 0.00154999999999958,
0.00384999999999991, 0.0077000000000059, -0.00770000000003193,
-0.0200899999999983, -0.0193000000000423, -0.0347000000000634,
-0.0540000000000927, -0.0733500000001364, -0.0501500000001637,
-0.0424500000000886, -0.050200000000087, -0.0308500000000459,
0.00384999999999834, -0.00231000000000208, -0.00387000000000167,
0.0030799999999978, -0.00385000000000757, -0.00385000000001064,
-0.0192500000000504, -0.0115500000000296, -0.0231500000001104,
-0.0579000000001085, -0.0733500000001314, -0.0386000000000697,
-0.0386000000000754, -0.0347500000000935, -0.00775000000001395,
0.00385000000000881, 0.000769999999999982, 0.0115500000000203,
0.00390000000001095, 0.00154000000000294, -0.00385000000001497,
-0.00385000000000567, -0.0309000000001234, -0.0347500000000728,
-0.0193000000000814, -0.0424500000000992, -0.0347500000000678,
0.274000000000822, 0.463150000000818, 1.03820000000353, 0.636800000000563,
-0.13663, -0.87225000000281, 0.644550000001354, -0.0579000000003174,
-0.72560000000209, -0.115800000000169, 2.08025000000553, -0.208400000000342,
-0.227700000000415, -0.328050000000636, -0.169850000000303, -0.104200000000212,
-0.0656500000001349, -0.0656500000001373, -0.0424500000000712,
-0.0347500000000697, -0.0285600000000002, -0.0193000000000324,
-0.0270000000000538, -0.0193000000000498, -0.0270000000000513,
-0.00849999999999724, -0.00770000000001513, -0.0162100000000009,
-0.0339800000000025, -0.0502000000001566, -0.0501500000000907,
-0.0193000000000454, -0.00770000000001893, 0.00385000000001136,
0.00390000000001402, 0.00153999999999996, -0.00307999999999993,
0.00390000000000023, 0.00384999999999834, 0.00384999999999644,
0.00385000000002943, -0.0138899999999971, -0.0223899999999993,
-0.0270500000000588, -0.00618999999999943, -0.0270500000000669,
0.00153999999999892, -0.000779999999999603, -2.5238009638656e-15,
0.00465000000000089, -0.00770000000001703, -2.91289464345889e-16,
0.00461999999999805, -0.0115900000000011, -0.00390000000001506,
-0.019300000000043, -0.0115899999999989, -0.0115900000000011,
-0.00770000000003258, 0, 0.00390000000000331, 0.0193000000000281,
0.00385000000002044, 0.00770000000002145, 0.00770000000000148,
0.0077000000000078, 0, 0.00308000000000135, -6.15348059644161e-15,
-0.015450000000036, -0.0309000000000726, -0.00385000000001254,
-0.0154000000000341, -1.11274169835756e-14, -0.00923999999999978,
-0.00234000000000107, -0.00770999999999944, 0.00385000000003251,
0.00461999999999429, 0.00385999999999811, -0.00770000000000798,
-0.023150000000093, -0.0154500000000348, -0.0424500000000737,
-0.019300000000043, -0.0308500000000125, -0.0309000000001054,
-0.0231500000000394, -1.1787959787484e-15, 0.000790000000000646,
-0.00231000000000036, 0, -0.00307999999999851, -0.00390000000002326,
-0.00230999999999753, -0.0193100000000022, -0.042450000000016,
-0.0385500000000679, -0.057900000000106, -0.0347000000000627,
-0.0386000000000922, -0.00385000000000445, 0.0077500000000097,
0.00230999999999995, -0.00385000000000352, 0.00307999999999948,
-0.000769999999999381, -1.1787959787484e-15, -0.015440000000001,
-0.0193000000000099, -0.0425000000000806, -0.0386000000000829,
-0.0424500000001675, -0.0386000000000773, -0.0463000000000192,
-0.00385000000001562, 0, 0.00769999999999875, -3.07674029821757e-15,
-0.00307999999999922, -0.0030799999999978, -0.0154000000000493,
-0.00385000000001254, -0.0231500000000079, -0.0347500000000802,
-0.0231500000000319, -0.0355200000000003, -0.0386000000000829,
-0.0463500000000801, -0.0347500000000678, 0.00155999999999792,
0.00385000000000639, -0.00385000000000231, 0, -0.00385000000000946,
-0.00153999999999966, 0, -0.0285600000000002, -0.0309000000000546,
-0.069450000000125, -0.0502000000000889, -0.0502000000000896,
0.3898000000001, 0.0540500000001028, 0.0115500000000253, 0.0116000000000142,
0.000769999999999981, -0.00385000000000504, -7.40090066366128e-15,
-0.00230999999999995, 0.00385000000000141, 0.00385000000000639,
-0.00385000000001254, -0.0270199999999981, -0.0502000000000896,
0.409100000000679, 0.0386000000000674, -0.0116000000000445, -0.00775000000004081,
-0.00307999999999993, -0.00385000000000757, -0.00770000000000208,
-0.00385000000001562, -0.00385000000001064, -0.00153999999999783,
-8.05101707233625e-15, -0.0177499999999995, -0.0424500000001796,
-0.0509500000000003, -0.0694500000001324, -0.0424500000001587,
-0.061750000000121, -0.0232000000000369, -0.0131299999999989,
-0.00153999999999886, 0.0077000000000059, -0.00310999999999879,
0.00769999999999982, 7.49841812496252e-15, -8.05101707233625e-15,
-0.0077000000000406, -0.0424500000000787, -0.0502000000001032,
-0.0347500000000747, -0.0656000000001262, -0.0733000000001494,
-0.034700000000074, -0.0193000000000869, 0.0231500000000662,
-0.00385000000000757, 0.00770000000001088, 0.0115600000000001,
-0.957150000001501, -0.14670000000027, -0.0772000000001383, -0.0617500000002002,
-0.0463000000000981, -0.0617500000001229, -0.0270000000000544,
-0.0347500000000597, -0.0386000000001412, -0.0694500000001331,
-0.0887500000001619, -0.0386000000000747, -0.0077500000000295,
0.015400000000064, 0.355050000000611, 0.0478699999999975, -6.15348059644161e-15,
-0.0177800000000019, -0.00385000000001064, -0.0116000000000674,
-0.0154500000000435, -0.0524900000000017, -0.0540500000002006,
-0.0540500000001021, -0.0617500000002394, -0.0308500000000688,
-0.0193000000000355, -0.0154000000000216, -0.000770000000000204,
0.00770000000001775, 0.00694000000000017, -0.00385000000001254,
0.0116000000000123, -0.00385000000002063, -0.00385000000000757,
-0.0270000000000743, -0.0309000000000763, -0.0926500000001775,
-0.0887500000003161, -0.0656500000001199, -0.042450000000078,
-0.0270000000000588, -0.0116000000000364, -0.000769999999999982,
-0.00385000000001254, -0.015400000000026, 0.00385000000002041,
0.0115500000000253, 0, -0.00385000000001161, -0.0386000000000723,
-0.0154500000000693, -0.100350000000196, -0.0849000000001704,
-0.0926500000001751, -0.0115500000000116, 0.00385000000000546,
-0.0116000000000154, 6.87160777622118e-15, -0.00384999999999991,
0.00231999999999886, -3.07674029821757e-15, 0.00390000000003514,
0.000779999999996745, -3.07674029821757e-15, -0.0231500000000617,
-0.0270500000000527, -0.0517200000000003, -0.050150000000105,
-0.0347500000000721, -0.0347500000000142, -0.00385000000001161,
0.00770000000000401, -0.00385000000000197, 0.000769999999999982,
-0.00385000000001372, 0.00385000000000141, 0.0116000000000278,
-3.71670324204166e-15, -0.0116000000000584, -0.00385000000001064,
-0.00464999999999875, -0.00775000000004982, -0.00390000000001506,
0.277900000000906, 0.119650000000208, 0.054000000000013, 0.0463000000000931,
0.0154500000000168, 0.00775000000000384, 0.0115500000000154,
0.00769999999999875, 1.89760393249092e-15, 0.00231999999999957,
0.000769999999999304, -0.0231500000000085, -0.0270500000000402,
0.351200000000562, -0.0231500000000833, -0.0270500000000588,
-0.0463500000000216, -0.0139000000000062, -9.23022089465272e-15,
-8.05101707233625e-15, 0.00385000000000546, 0.000759999999998229,
-0.0115500000000395, 0.000769999999999982, -0.011600000000024,
-0.00770000000001206, -0.0540500000001929, -0.0772000000001558,
-0.0656000000000217, -0.0772000000001484, -0.0579000000001128,
-0.0347000000000764, -0.0193000000000461, -0.00385000000000352,
-0.00385000000002122, -0.00696000000000083, 0.000789999999999225,
0.00384999999999834, -0.000800000000000978, -0.0116000000000234,
-0.00775000000001088, -0.0115900000000055, -0.0193000000000218,
-0.0347500000000808, -0.0386000000000897, -0.0501500000000858,
-0.00233999999999881, -0.00385000000000757, 2.00000000009208e-05,
0.308750000000515, 0.092650000000154, 0.0424500000000756, 0.0231500000000227,
0.0154500000000312, -0.00385000000001469, 0.00538999999999237,
0.474750000000936, 0.212300000000357, -0.0030699999999996, -0.0309000000000739,
-0.0115500000000265, -0.0116000000000265, -3.57390000000716,
-0.293350000001048, -0.119650000000226, -0.104200000000194, -0.0926500000001831,
-0.0540500000001096, -0.0694500000002714, -0.0772000000001527,
-0.0965000000001976, -0.0694500000001375, -0.100350000000182,
-0.084950000000289, -0.061750000000121, -0.0425000000000912,
-0.0424500000000662, -0.00770000000002011, -0.0154500000000422,
-0.00307999999999993, -0.00230999999999994, 0.00385000000001447,
-0.00154, -0.00385000000000567, -0.0386000000000747, -0.0695000000002463,
-0.0772000000001664, -0.0849000000002961, -0.0887500000001668,
-0.0193000000000504, -0.0578500000001047, -0.00775000000000708,
-1.2095231788207e-14, 0.00848999999999485, -3.07674029821757e-15,
-0.00541000000000057, -0.00390000000002247, 0.000769999999999981,
-0.0293300000000002, -0.050200000000087, -0.0656000000002546,
-0.0540500000001096, -0.069450000000138, 0.123500000000375, 0.0849000000001387,
0.00384999999999644, 0.023200000000042, 0.0115500000000123, 0.00775000000000473,
0.0115500000000203, 0.00385000000001447, -0.00775000000002506,
0.00466000000000122, -0.0254699999999978, -0.054799999999998,
-0.0231500000000444, 0.0116000000000454, 0.115800000000206, 0.030900000000046,
0.00385000000000331, -0.00153999999999996, 0.00384999999999084,
-0.00385000000000757, 0.00770000000001088, 1.7849988639723e-14,
0.00230999999999994, 0.00385000000001326, -0.00153999999999882,
-0.038600000000126, -0.0309000000000553, -0.00692999999999628,
-0.0154000000000403, -0.0579000000001097, -0.0347500000000678,
-0.0100400000000054, 0.00385000000000023, -0.00385000000001994,
-2.17923926727129e-14, 0.00389999999999028, 0.00390000000001402,
0.00384999999999084, -0.00385000000001751, 0.00770000000001399,
-0.0308500000000632, -0.0502000000001986, -0.0695000000001394,
-0.0501799999999982, -0.0309000000000752, -0.0270500000000557,
-0.0100500000000011, 0.00389999999999596, 0.0116000000000117,
1.89760393249092e-15, 0.0115500000000123, 0.00384999999998841,
-0.00385000000002965, 0.0077000000000078, 0.00385000000000639,
0.00770000000000283, -0.0501500000001132, -0.0617500000002242,
-0.0710100000000004, -0.0810500000000306, -0.0540500000001891,
-0.0386000000000617, -0.019300000000043, 0.00775000000000473,
0.00847000000000282, 0.00462999999999951, -2.11128370304365e-14,
0.00770000000001088, 0.00384999999999858, 9.99999999962123e-06,
-0.00770000000001206, -0.0733000000000254, -0.0656000000001967,
-0.111900000000213, -0.100350000000323, -0.0579000000001141,
-0.0385500000000131, -0.0116000000000215, 0.0193000000000318,
0.00390000000001402, 0.0270000000000452, 0.00770000000000182,
-8.05101707233625e-15)), row.names = c(NA, -717L), class = "data.frame")
As others have said, it is not clear what you are looking for.
specifically, it's not clear how high above "baseline" is too high.
Here's a shot at it:
df_prime <- df$X2[-1] - df$X2[-length(df$X2)]
large_rise <- which(df_prime > sd(df_prime) & df$X2[-length(df$X2)] > -sd(df$X2))
df$X1[large_rise]
It's difficult to know from the question, but aren't you just looking for something like this?
spikes <- as.numeric(df$X1[df$X2 > 0.1])
spikes <- spikes[which(diff(c(0, spikes)) > 3)]
spikes
#> [1] 2738 2758 2984 2994 3126 3139 3190 3260 3273 3309 3316 3363 3377
So, for example if you did
plot(df$X1, df$X2, type = "l")
points(spikes, rep(1, length(spikes)), col="red")
You would get

What is the format of "{123, affdsf, 223, 22, dgbwa, 33333}"?

I have the following format, please advise how to convert it to a list in R?
"{1948, 2507, 2510, 7030, 7110, 9009, 00027, 00206, 00399, 00717, 00814, 00828, 00848, 00917, 01050, 01105, 01144, 02130, 02768, 03037, 03752, 03754, 04070, 04110, 05050, 05255, 05289, 05564, 05595, 06100, 06330, 06671, 07041, 07119, 07137, 07273, 07313, 07454, 07871, 08104, 08714, 08726, 08995, 09059, 09073, 09525, 09949, 09981, 10092, 10439, 10782, 11185, 11507, 11712, 11806, 11858, 11980, 12067, 12113, 12139, 12643, 13820, 14534, 15007, 15014, 15549, 15953, 16151, 16174, 16634, 16733, 16888, 17111, 17207, 17377, 17721, 17900, 18118, 18400, 18686, 18880, 19080, 19342, 19444, 19772, 19790, 19891, 20091, 20245, 20402, 20811, 21114, 21345, 21811, 21881, 22222, 22311, 22320, 22831, 22969, 23251, 23572, 23734, 23862, 23889, 24034, 24463, 25172, 25688, 26143, 26221, 26803, 26850, 26898, 27497, 28291, 28343, 29411, 29419, 30024, 30561, 30923, 31345, 31351, 31555, 31927, 32198, 32861, 33020, 33040, 33095, 33188, 33311, 33368, 33377, 33475, 33519, 33574, 33592, 34207, 34235, 34272, 34484, 34854, 34872, 34875, 34876, 34880, 35222, 35292, 35344, 36177, 36266, 37038, 37060, 37548, 37686, 37700, 38139, 39368, 39369, 39633, 40132, 40698, 40704, 40744, 40819, 41311, 41971, 42102, 42616, 43055, 43211, 43234, 43428, 43494, 43934, 44117, 44252, 44272, 44301, 44336, 44619, 44866, 44888, 45049, 45197, 45412, 45718, 46694, 46736, 47000, 48046, 48540, 49078, 49109, 49216, 49388, 49464, 50056, 50155, 50217, 50477, 50692, 51122, 51445, 51946, 52475, 52537, 52982, 54011, 54031, 54160, 54963, 55000, 55537, 56080, 56163, 56282, 56760, 56787, 57102, 57727, 57871, 58101, 58558, 58882, 59902, 60225, 60397, 60501, 60619, 60703, 60890, 61075, 61894, 61944, 62322, 62337, 62380, 62413, 62729, 62766, 62923, 63010, 63234, 63977, 64127, 65359, 65428, 65542, 65750, 65863, 66184, 66636, 66712, 67201, 67439, 67953, 68133, 68854, 69251, 69959, 70107, 70725, 70768, 71081, 71099, 71948, 72013, 72377, 72400, 72420, 72735, 73000, 73015, 73142, 73223, 73455, 73717, 74049, 74492, 74854, 74941, 75142, 75399, 75464, 75587, 75618, 75642, 75887, 76357, 76651, 77199, 77302, 77456, 77579, 77601, 77649, 77668, 77694, 77745, 78006, 78010, 78178, 78335, 78656, 78729, 78808, 78824, 78844, 78945, 79416, 79471, 79915, 80077, 80111, 80189, 80262, 80409, 80470, 80529, 80539, 80838, 81272, 81513, 81658, 81740, 81743, 81762, 81843, 82001, 82070, 82106, 82342, 82472, 82719, 83670, 84009, 84151, 84299, 84430, 84450, 84460, 84945, 86411, 86443, 86446, 86668, 86942, 87286, 87317, 87624, 87785, 88023, 88517, 88696, 88787, 88868, 88977, 89206, 90108, 90440, 90734, 90802, 90849, 90920, 90931, 91011, 91031, 91133, 91777, 91949, 92162, 92494, 93012, 93172, 94300, 94517, 95142, 95410, 95559, 95859, 96112, 97255, 97787, 97986, 98240, 98817, 99050, 99198, 99222, 99241, 99295, 99326, 99335, 99503, 99603, 99643, 99803, 99968}"
THIS IS NOT A DUPLICATE OF convert json to list in a vectorized way in R
IT'S COMPLETELY DIFFERENT BECAUSE THE FORMAT IS ABSOLUTELY DIFFERENT.
Try this one line code:
as.numeric(sapply(strsplit(substr(j,2,nchar(j)-1),split = ","),trimws))
[1] 1948 2507 2510 7030 7110 9009 27 206 399 717 814 828 848 917 1050 1105 1144
[18] 2130 2768 3037 3752 3754 4070 4110 5050 5255 5289 5564 5595 6100 6330 6671 7041 7119
[35] 7137 7273 7313 7454 7871 8104 8714 8726 8995 9059 9073 9525 9949 9981 10092 10439 10782
[52] 11185 11507 11712 11806 11858 11980 12067 12113 1213 ..
Your input:
j<-"{1948, 2507, 2510, 7030, 7110, 9009, 00027, 00206, 00399, 00717, 00814, 00828, 00848, 00917, 01050, 01105, 01144, 02130, 02768, 03037, 03752, 03754, 04070, 04110, 05050, 05255, 05289, 05564, 05595, 06100, 06330, 06671, 07041, 07119, 07137, 07273, 07313, 07454, 07871, 08104, 08714, 08726, 08995, 09059, 09073, 09525, 09949, 09981, 10092, 10439, 10782, 11185, 11507, 11712, 11806, 11858, 11980, 12067, 12113, 12139, 12643, 13820, 14534, 15007, 15014, 15549, 15953, 16151, 16174, 16634, 16733, 16888, 17111, 17207, 17377, 17721, 17900, 18118, 18400, 18686, 18880, 19080, 19342, 19444, 19772, 19790, 19891, 20091, 20245, 20402, 20811, 21114, 21345, 21811, 21881, 22222, 22311, 22320, 22831, 22969, 23251, 23572, 23734, 23862, 23889, 24034, 24463, 25172, 25688, 26143, 26221, 26803, 26850, 26898, 27497, 28291, 28343, 29411, 29419, 30024, 30561, 30923, 31345, 31351, 31555, 31927, 32198, 32861, 33020, 33040, 33095, 33188, 33311, 33368, 33377, 33475, 33519, 33574, 33592, 34207, 34235, 34272, 34484, 34854, 34872, 34875, 34876, 34880, 35222, 35292, 35344, 36177, 36266, 37038, 37060, 37548, 37686, 37700, 38139, 39368, 39369, 39633, 40132, 40698, 40704, 40744, 40819, 41311, 41971, 42102, 42616, 43055, 43211, 43234, 43428, 43494, 43934, 44117, 44252, 44272, 44301, 44336, 44619, 44866, 44888, 45049, 45197, 45412, 45718, 46694, 46736, 47000, 48046, 48540, 49078, 49109, 49216, 49388, 49464, 50056, 50155, 50217, 50477, 50692, 51122, 51445, 51946, 52475, 52537, 52982, 54011, 54031, 54160, 54963, 55000, 55537, 56080, 56163, 56282, 56760, 56787, 57102, 57727, 57871, 58101, 58558, 58882, 59902, 60225, 60397, 60501, 60619, 60703, 60890, 61075, 61894, 61944, 62322, 62337, 62380, 62413, 62729, 62766, 62923, 63010, 63234, 63977, 64127, 65359, 65428, 65542, 65750, 65863, 66184, 66636, 66712, 67201, 67439, 67953, 68133, 68854, 69251, 69959, 70107, 70725, 70768, 71081, 71099, 71948, 72013, 72377, 72400, 72420, 72735, 73000, 73015, 73142, 73223, 73455, 73717, 74049, 74492, 74854, 74941, 75142, 75399, 75464, 75587, 75618, 75642, 75887, 76357, 76651, 77199, 77302, 77456, 77579, 77601, 77649, 77668, 77694, 77745, 78006, 78010, 78178, 78335, 78656, 78729, 78808, 78824, 78844, 78945, 79416, 79471, 79915, 80077, 80111, 80189, 80262, 80409, 80470, 80529, 80539, 80838, 81272, 81513, 81658, 81740, 81743, 81762, 81843, 82001, 82070, 82106, 82342, 82472, 82719, 83670, 84009, 84151, 84299, 84430, 84450, 84460, 84945, 86411, 86443, 86446, 86668, 86942, 87286, 87317, 87624, 87785, 88023, 88517, 88696, 88787, 88868, 88977, 89206, 90108, 90440, 90734, 90802, 90849, 90920, 90931, 91011, 91031, 91133, 91777, 91949, 92162, 92494, 93012, 93172, 94300, 94517, 95142, 95410, 95559, 95859, 96112, 97255, 97787, 97986, 98240, 98817, 99050, 99198, 99222, 99241, 99295, 99326, 99335, 99503, 99603, 99643, 99803, 99968}"
This code removes first and last character of the string ("{" and "}" characters), splits values by "," and removes whitespaces using trimws. After that it moves the format to number.
If it happens your data actually is json, stick with the rjson package. This answer is assuming your data is not json (since rjson::fromjson throws an error on your data)
Try:
string <- "{1948, 2507, 2510, 7030, 7110, 9009, 00027, 00206, 00399, 00717, 00814, 00828, 00848, 00917, 01050, 01105, 01144, 02130, 02768, 03037, 03752, 03754, 04070, 04110, 05050, 05255, 05289, 05564, 05595, 06100, 06330, 06671, 07041, 07119, 07137, 07273, 07313, 07454, 07871, 08104, 08714, 08726, 08995, 09059, 09073, 09525, 09949, 09981, 10092, 10439, 10782, 11185, 11507, 11712, 11806, 11858, 11980, 12067, 12113, 12139, 12643, 13820, 14534, 15007, 15014, 15549, 15953, 16151, 16174, 16634, 16733, 16888, 17111, 17207, 17377, 17721, 17900, 18118, 18400, 18686, 18880, 19080, 19342, 19444, 19772, 19790, 19891, 20091, 20245, 20402, 20811, 21114, 21345, 21811, 21881, 22222, 22311, 22320, 22831, 22969, 23251, 23572, 23734, 23862, 23889, 24034, 24463, 25172, 25688, 26143, 26221, 26803, 26850, 26898, 27497, 28291, 28343, 29411, 29419, 30024, 30561, 30923, 31345, 31351, 31555, 31927, 32198, 32861, 33020, 33040, 33095, 33188, 33311, 33368, 33377, 33475, 33519, 33574, 33592, 34207, 34235, 34272, 34484, 34854, 34872, 34875, 34876, 34880, 35222, 35292, 35344, 36177, 36266, 37038, 37060, 37548, 37686, 37700, 38139, 39368, 39369, 39633, 40132, 40698, 40704, 40744, 40819, 41311, 41971, 42102, 42616, 43055, 43211, 43234, 43428, 43494, 43934, 44117, 44252, 44272, 44301, 44336, 44619, 44866, 44888, 45049, 45197, 45412, 45718, 46694, 46736, 47000, 48046, 48540, 49078, 49109, 49216, 49388, 49464, 50056, 50155, 50217, 50477, 50692, 51122, 51445, 51946, 52475, 52537, 52982, 54011, 54031, 54160, 54963, 55000, 55537, 56080, 56163, 56282, 56760, 56787, 57102, 57727, 57871, 58101, 58558, 58882, 59902, 60225, 60397, 60501, 60619, 60703, 60890, 61075, 61894, 61944, 62322, 62337, 62380, 62413, 62729, 62766, 62923, 63010, 63234, 63977, 64127, 65359, 65428, 65542, 65750, 65863, 66184, 66636, 66712, 67201, 67439, 67953, 68133, 68854, 69251, 69959, 70107, 70725, 70768, 71081, 71099, 71948, 72013, 72377, 72400, 72420, 72735, 73000, 73015, 73142, 73223, 73455, 73717, 74049, 74492, 74854, 74941, 75142, 75399, 75464, 75587, 75618, 75642, 75887, 76357, 76651, 77199, 77302, 77456, 77579, 77601, 77649, 77668, 77694, 77745, 78006, 78010, 78178, 78335, 78656, 78729, 78808, 78824, 78844, 78945, 79416, 79471, 79915, 80077, 80111, 80189, 80262, 80409, 80470, 80529, 80539, 80838, 81272, 81513, 81658, 81740, 81743, 81762, 81843, 82001, 82070, 82106, 82342, 82472, 82719, 83670, 84009, 84151, 84299, 84430, 84450, 84460, 84945, 86411, 86443, 86446, 86668, 86942, 87286, 87317, 87624, 87785, 88023, 88517, 88696, 88787, 88868, 88977, 89206, 90108, 90440, 90734, 90802, 90849, 90920, 90931, 91011, 91031, 91133, 91777, 91949, 92162, 92494, 93012, 93172, 94300, 94517, 95142, 95410, 95559, 95859, 96112, 97255, 97787, 97986, 98240, 98817, 99050, 99198, 99222, 99241, 99295, 99326, 99335, 99503, 99603, 99643, 99803, 99968}"
string as list of characters:
string_as_list_char <- as.list(strsplit(gsub('\\{|\\}', '', string), ", "))[[1]]
or converted to numeric:
string_as_list_num <- as.list(as.numeric(strsplit(gsub('\\{|\\}', '', string), ", ")[[1]]))

Fitting exponential decay and pulling out time constant value in R

I'm trying to fit some curves from the data below with a mono-exponential "decay". Graphical display is not as important as is pulling out the time constant. the y-axis is pA and the x is time in seconds.
dput(stackover_data)
structure(list(Time = c(0.09990001, 0.19990001, 0.29990001, 0.39990001,
0.49990001, 0.59990001, 0.69990001, 0.79990001, 0.89990001, 0.99990001,
1.09990001, 1.19990001, 1.29990001, 1.39990001, 1.49990001, 1.59990001,
1.69990001, 1.79990001, 1.89990001, 1.99990001, 2.09990001, 2.19990001,
2.29990001, 2.39990001, 2.49990001, 2.59990001, 2.69990001, 2.79990001,
2.89990001, 2.99990001, 3.09990001, 3.19990001, 3.29990001, 3.39990001,
3.49990001, 3.59990001, 3.69990001, 3.79990001, 3.89990001, 3.99990001,
4.09990001, 4.19990001, 4.29990001, 4.39990001, 4.49990001, 4.59990001,
4.69990001, 4.79990001, 4.89990001, 4.99990001, 5.09990001, 5.19990001,
5.29990001, 5.39990001, 5.49990001, 5.59990001, 5.69990001, 5.79990001,
5.89990001, 5.99990001, 6.09990001, 6.19990001, 6.29990001, 6.39990001,
6.49990001, 6.59990001, 6.69990001, 6.79990001, 6.89990001, 6.99990001,
7.09990001, 7.19990001, 7.29990001, 7.39990001, 7.49990001, 7.59990001,
7.69990001, 7.79990001, 7.89990001, 7.99990001, 8.09990001, 8.19990001,
8.29990001, 8.39990001, 8.49990001, 8.59990001, 8.69990001, 8.79990001,
8.89990001, 8.99990001, 9.09990001, 9.19990001, 9.29990001, 9.39990001,
9.49990001, 9.59990001, 9.69990001, 9.79990001, 9.89990001, 9.99990001,
10.09990001, 10.19990001, 10.29990001, 10.39990001, 10.49990001,
10.59990001, 10.69990001, 10.79990001, 10.89990001, 10.99990001,
11.09990001, 11.19990001, 11.29990001, 11.39990001, 11.49990001,
11.59990001, 11.69990001, 11.79990001, 11.89990001, 11.99990001,
12.09990001, 12.19990001, 12.29990001, 12.39990001, 12.49990001,
12.59990001, 12.69990001, 12.79990001, 12.89990001, 12.99990001,
13.09990001, 13.19990001, 13.29990001, 13.39990001, 13.49990001,
13.59990001, 13.69990001, 13.79990001, 13.89990001, 13.99990001,
14.09990001, 14.19990001, 14.29990001, 14.39990001, 14.49990001,
14.59990001, 14.69990001, 14.79990001, 14.89990001, 14.99990001,
15.09990001, 15.19990001, 15.29990001, 15.39990001, 15.49990001,
15.59990001, 15.69990001, 15.79990001, 15.89990001, 15.99990001,
16.09990001, 16.19990001, 16.29990001, 16.39990001, 16.49990001,
16.59990001, 16.69990001, 16.79990001, 16.89990001, 16.99990001,
17.09990001, 17.19990001, 17.29990001, 17.39990001, 17.49990001,
17.59990001, 17.69990001, 17.79990001, 17.89990001, 17.99990001,
18.09990001, 18.19990001, 18.29990001, 18.39990001, 18.49990001,
18.59990001, 18.69990001, 18.79990001, 18.89990001, 18.99990001,
19.09990001, 19.19990001, 19.29990001, 19.39990001, 19.49990001,
19.59990001, 19.69990001, 19.79990001, 19.89990001, 19.99990001
), `Trace 1` = c(-3.08656892325052, 9.36821982641837, 8.13806079083122,
10.7039590839898, 7.25670468903547, 4.31122291688919, 1.77905971163193,
-6.27606834721828, -8.65955381985049, -10.1445673910916, -10.6649772153892,
-6.52301948183154, -11.9757817854835, -0.976254254762154, 0.237467076202677,
-11.8114896779541, -11.0022757370468, -16.1845427042923, 2.70565927469852,
-18.9048281652216, -22.153682283437, -4363.32044948884, -3470.59111611883,
-2877.51064886248, -2253.51505229908, -1876.62974792002, -1541.14546478629,
-1288.26617158403, -1232.52313999354, -1042.98549163259, -938.795146277054,
-810.913567086442, -736.390770574588, -682.044521632168, -638.575324886466,
-596.587948389699, -629.282103146111, -569.200610245336, -587.387817942122,
-521.939307772762, -533.776693538631, -514.782451411908, -445.949395199026,
-451.026618716539, -461.669600651513, -442.372477296489, -426.132547857502,
-452.471101919398, -449.377497412324, -436.817659066873, -487.464805660851,
-442.915455035179, -458.666741489705, -454.990437793055, -455.173154690614,
-426.885702219019, -456.985443835707, -408.869318611773, -442.418742303429,
-407.42593099033, -414.538253423006, -316.036755461507, -248.395118743017,
-198.708015370115, -183.88480278352, -160.041754268138, -139.020785864805,
-123.701641615743, -137.253323547789, -124.240619047461, -121.512859107349,
-113.719386521321, -102.98429740535, -118.721098087137, -109.789471870234,
-122.145399997255, -109.542467905009, -96.6725326170008, -108.16233877188,
-94.1092968366083, -88.0987495250118, -89.2245425850472, -96.3495439993499,
-110.340434956898, -98.8777265479938, -88.0674013452629, -102.755317774957,
-91.0752842494157, -107.959830375198, -78.8424385901398, -93.7743443479161,
-98.3591889757604, -72.3214090579355, -85.6296201608712, -112.596656084088,
-115.518068650615, -110.973655632476, -78.8021307215932, -85.6443070182152,
-93.1744356389988, -100.483987323044, -87.6672104421484, -83.6481077757535,
-82.1465876740715, -86.7838666454595, -75.9066755520263, -86.6416980998645,
-88.9405806921788, -74.0592581080291, -86.3433338300531, -93.5114839187431,
-91.1875849041866, -82.7018083540351, -102.859075734953, -82.1494206590809,
-79.2197323780198, -78.1558787387238, -86.2649418863144, -93.5271994290692,
-84.6678528566242, -71.4828270654073, -69.5618263581887, -67.2920558863641,
-58.1490330439793, -59.8163238740351, -73.6957797622946, -61.7673947702343,
-63.6492255747164, -64.043638367468, -73.8301991524909, -69.3055992018769,
-65.9342860783478, -74.9891521715357, -74.5779808619617, -69.9029875902787,
-69.746935396023, -71.35455530782, -73.5279471991205, -77.8000626250279,
-74.6065254864801, -64.0834786591292, -45.1346212136811, -43.1615385011179,
-42.554841323715, -50.276566542849, -56.1940469314277, -49.6368473019083,
-53.9842269565738, -54.1480156577708, -48.2160751112714, -43.2902743874793,
-46.4358385791469, -58.4459814967147, -49.0443619038303, -56.8007684056415,
-56.3345802020277, -40.1704617527471, -38.2624410086947, -39.9339279963857,
-27.7637250414188, -40.8686520798649, -30.9246406597275, -32.70021580322,
-28.7536071219405, -32.4403271637661, -30.3389870650563, -23.2866770834185,
-40.349370835336, -39.7478049410975, -38.4648592612063, -37.4191914226093,
-52.1259539643142, -38.0993644763616, -37.5493511061199, -25.0794144286873,
-32.6165749053184, -17.2616237291667, -32.1515419380766, -27.0814392288745,
-26.2440551871993, -17.5882979851251, -19.852012057918, -22.1408312947152,
-21.3395092026716, -28.0789344732281, -19.6443281019371, -26.6585778398899,
-24.2994817465722, -27.847437388783, -22.0565059455712, -24.8747905836351,
-30.7544250721296, -29.9924061105416, -22.5728855329732, -24.9767037068316,
-18.8208568928653, -10.7306087216159, -16.4281210876173, -19.3057174287183,
-15.9745523586581), `Trace 2` = c(5.94927992143286, 1.42121402161905,
6.78788136514507, 3.33970424403748, -3.73956433922802, -7.3097330836793,
-9.18242380095097, 3.29952017048882, 5.17208246028, 1.53238537592179,
6.90832098860733, 3.16748380079213, 5.49988319742749, 3.86758484926656,
11.981378748128, -0.208159377585758, -7.2781503569274, 1.71389537416221,
-4.77614396689646, -0.561115871778583, -14.5385068323377, -4944.63006397872,
-3891.23141079918, -3065.66300899921, -2527.61853791531, -2060.05223709386,
-1763.27814995793, -1451.77884853301, -1368.42409410389, -1115.3385743493,
-990.942590410893, -852.951182088994, -920.516421149141, -860.486324532349,
-751.257935697359, -768.757880984424, -713.935725464811, -715.011716140783,
-616.355633698422, -672.133827760044, -629.418857335266, -607.342562427532,
-599.657392949772, -553.265193759536, -572.878306401819, -583.153736972572,
-589.661926862833, -527.552306430313, -553.672277977987, -539.906254466276,
-525.491319968336, -564.876755897633, -505.363603776286, -575.649358123809,
-497.897048596953, -544.360501913781, -483.501835830955, -533.085516954168,
-478.007776808595, -453.333986756674, -447.125338584774, -380.268661132557,
-253.127091546528, -227.877427383931, -191.488369613846, -166.799480062655,
-171.446527550207, -156.775073739292, -148.804014388301, -151.79670303645,
-152.599645619669, -113.40778617347, -133.161498039771, -129.929086626311,
-156.827022418459, -146.618934465797, -94.20997521855, -124.514491421933,
-89.8538131075507, -91.6500841191965, -113.887119052802, -109.971138752404,
-122.532101557384, -107.714865018275, -120.258046104427, -133.296582343699,
-111.726093380577, -121.533575165211, -93.2177144160483, -97.3820468959891,
-111.294540122284, -100.913019601926, -90.8456358331201, -94.143801546019,
-89.9179990101121, -79.3197988311624, -87.0020882554621, -103.315348613102,
-92.6091563297179, -87.3900461813473, -106.61173718685, -117.18534688083,
-106.417237368758, -114.977458547258, -101.759575332149, -93.2156781534656,
-79.4452885332048, -83.8035016923155, -85.7770898942133, -80.151862850598,
-72.4787832080729, -75.4887322569144, -70.8074563945233, -76.9714115362765,
-74.2305756099668, -72.7994824773122, -86.4890002605112, -68.5241155035978,
-76.723559709657, -60.5721619300473, -64.185161607482, -81.0777473166996,
-59.7023719535658, -48.0615168793405, -63.7668096308392, -69.0412888697281,
-70.9894443873153, -48.8366672284658, -43.1003899300401, -50.5591327356918,
-65.184353265464, -52.2850955061746, -66.0074402595226, -69.3081002474696,
-69.3876079446205, -53.0919105504865, -69.3289906081927, -63.5198462609014,
-70.9316181916525, -68.753726760587, -57.1360889764902, -45.7297495037924,
-45.0225501894778, -45.7487568762369, -46.486850874694, -46.7437348684622,
-41.1396209831975, -38.5056250833188, -49.8076229939374, -46.9868446480308,
-38.4639795698766, -40.8336417843587, -49.9810721301961, -58.7820616810459,
-51.4348042554539, -56.9132093579117, -53.527642322397, -49.7620292214747,
-54.6821412367706, -66.7962460110546, -52.0791798888695, -43.3022792658504,
-47.5584813904168, -36.3352478868053, -39.8699383685185, -43.9973042247199,
-49.3191665577514, -42.0818922168797, -37.7538461067954, -29.1591142899914,
-31.6815992654018, -35.3664789321372, -29.6435427630652, -45.5528322348158,
-37.0568327299924, -29.6739001502498, -37.0952936191428, -44.1786405275305,
-50.929131558954, -31.7842698176079, -31.4300485665082, -33.960404707688,
-34.0566040705914, -41.2299273666286, -30.1094672132779, -28.6802701495622,
-37.1973277887207, -31.7114680610898, -32.5450840790921, -28.5823680785893,
-27.5268500574853, -15.2389866754792, -19.2079199191329, -28.9156878395334,
-16.5863741697772, -31.6638747515138, -34.679018047114, -32.6358481677933,
-26.3463300811612, -18.542719903123), `Trace 3` = c(4.74984904312473,
-6.83237755613888, -4.15774846489683, 0.0116753456567835, 4.3974208810568,
8.56175743226069, -0.271281722577697, -10.7982854290066, -18.31704442037,
-15.7577103721081, -20.5775487342614, -17.7307614062074, -14.2844531791322,
-5.43267691733291, 2.52755064598145, -0.79977817636393, -8.07074631773528,
-14.6631228094247, -9.70657043283029, -11.4521183354252, -10.8401698276907,
-5495.45466682125, -4324.53856178012, -3525.03574657791, -2774.37269062517,
-2175.8494069729, -1790.54797875842, -1574.71991002551, -1342.90235937345,
-1212.59715782746, -1042.56659527622, -1006.18885759924, -962.27473179716,
-870.459562514915, -847.89784200018, -770.205392585522, -709.279500982242,
-740.484104629636, -739.054108223511, -645.039948927494, -712.347389757369,
-609.734365947307, -613.120930696929, -630.805982834916, -581.828460576409,
-608.843958294059, -602.432567890874, -547.023947351376, -571.475614300591,
-561.707380041648, -535.094375334464, -578.783728464712, -535.290908514374,
-489.922390143035, -522.824368605682, -520.322259788742, -556.271028784585,
-536.474251127913, -546.811709881227, -541.304029428261, -563.702125503968,
-460.326259578172, -348.718859066443, -261.740363110507, -244.084411205067,
-182.533320181224, -205.356578064485, -157.759947570376, -162.214202634405,
-180.011654146528, -176.904445787135, -167.116794266109, -163.416234638164,
-155.821745831568, -134.760994564946, -131.702791057298, -138.598664148971,
-139.818370538306, -134.700400696581, -137.275258643317, -119.921721377143,
-118.187525264885, -118.490007264421, -130.391338033262, -138.05114734155,
-127.505366899025, -132.636233878671, -123.490111075107, -113.42942794286,
-122.40925776411, -114.48254277334, -122.78054774327, -90.9528981496313,
-96.9508488566491, -107.842237007564, -91.0521871208113, -91.0347011279681,
-85.7054147126896, -89.7363815316723, -77.6588521980595, -76.6733051688234,
-85.821987540449, -90.3216600489126, -66.1137192454051, -77.7157754909198,
-89.8637060331546, -80.9149315468257, -71.7434481475478, -97.6600776026528,
-103.405900069071, -83.7502802699966, -77.4600968679799, -77.8705250264031,
-78.9012879762878, -87.4698127883497, -92.9587198547098, -96.1222986385502,
-91.1990314226718, -83.8744116889207, -61.7720026226848, -83.6616594930646,
-91.3899042204887, -74.2552104995002, -94.3117384183545, -81.130091800586,
-87.6638514042855, -79.061763018934, -78.7472359255489, -72.9642062301352,
-83.5759889935287, -74.066505526926, -62.6191749912397, -75.7150349466975,
-71.8704211546101, -68.4798935028027, -77.0470926055507, -76.6024078920054,
-66.5760393943856, -77.8848130194484, -77.5151449198185, -65.5482413040489,
-47.6087884294611, -35.6865291924552, -47.9648700291328, -62.8176093048552,
-48.969044800301, -46.853228499419, -52.562083189922, -58.7677963803543,
-63.6615259259388, -51.7351340481719, -54.5967510836755, -54.1294816023731,
-47.7621427567758, -40.9244974392914, -50.5286026229961, -45.9961132210891,
-43.143208905187, -43.8395623054062, -49.3326618452772, -48.308625153823,
-44.9833219970479, -32.4206723427767, -45.6628898472981, -41.4748918817457,
-43.6438737242104, -42.9844450147366, -30.3729810016378, -44.9519080631137,
-45.8241111308495, -40.4766896430523, -40.3007872752484, -46.4770136239361,
-49.3759491415156, -46.0844075639024, -34.3090862431636, -26.6047381016158,
-28.9119815377167, -33.1619464253006, -37.3813739690468, -46.5001186141503,
-54.5235621985407, -44.5233400090119, -55.2272273265567, -48.088333647706,
-55.3522589332341, -52.8146474401922, -60.1877313269088, -48.2512741329643,
-34.8540879147507, -55.2019332852645, -50.8488894007021, -49.9600753927381,
-48.5654335180739, -47.8918651630979, -45.1405419376454, -40.8504490169926,
-38.5815843253789, -45.001677748311, -43.0547862406721)), .Names = c("Time",
"Trace 1", "Trace 2", "Trace 3"), row.names = c(1000L, 2000L,
3000L, 4000L, 5000L, 6000L, 7000L, 8000L, 9000L, 10000L, 11000L,
12000L, 13000L, 14000L, 15000L, 16000L, 17000L, 18000L, 19000L,
20000L, 21000L, 22000L, 23000L, 24000L, 25000L, 26000L, 27000L,
28000L, 29000L, 30000L, 31000L, 32000L, 33000L, 34000L, 35000L,
36000L, 37000L, 38000L, 39000L, 40000L, 41000L, 42000L, 43000L,
44000L, 45000L, 46000L, 47000L, 48000L, 49000L, 50000L, 51000L,
52000L, 53000L, 54000L, 55000L, 56000L, 57000L, 58000L, 59000L,
60000L, 61000L, 62000L, 63000L, 64000L, 65000L, 66000L, 67000L,
68000L, 69000L, 70000L, 71000L, 72000L, 73000L, 74000L, 75000L,
76000L, 77000L, 78000L, 79000L, 80000L, 81000L, 82000L, 83000L,
84000L, 85000L, 86000L, 87000L, 88000L, 89000L, 90000L, 91000L,
92000L, 93000L, 94000L, 95000L, 96000L, 97000L, 98000L, 99000L,
100000L, 101000L, 102000L, 103000L, 104000L, 105000L, 106000L,
107000L, 108000L, 109000L, 110000L, 111000L, 112000L, 113000L,
114000L, 115000L, 116000L, 117000L, 118000L, 119000L, 120000L,
121000L, 122000L, 123000L, 124000L, 125000L, 126000L, 127000L,
128000L, 129000L, 130000L, 131000L, 132000L, 133000L, 134000L,
135000L, 136000L, 137000L, 138000L, 139000L, 140000L, 141000L,
142000L, 143000L, 144000L, 145000L, 146000L, 147000L, 148000L,
149000L, 150000L, 151000L, 152000L, 153000L, 154000L, 155000L,
156000L, 157000L, 158000L, 159000L, 160000L, 161000L, 162000L,
163000L, 164000L, 165000L, 166000L, 167000L, 168000L, 169000L,
170000L, 171000L, 172000L, 173000L, 174000L, 175000L, 176000L,
177000L, 178000L, 179000L, 180000L, 181000L, 182000L, 183000L,
184000L, 185000L, 186000L, 187000L, 188000L, 189000L, 190000L,
191000L, 192000L, 193000L, 194000L, 195000L, 196000L, 197000L,
198000L, 199000L, 200000L), class = "data.frame")
I've tried doing lm(y~x)but it doesn't seem to get the right answer (verified the right answer in Igor) and obviously this is because its a linear model and not a exponential. Any all suggestions are welcomed. I'm struggling on this!
Thanks all!
names(DF) <- make.names(names(DF))
plot(Trace.1 ~ Time, data = DF)
#remove the initial values that clearly don't follow the model
DF1 <- DF[-seq_len(which((diff(DF$Trace.1) < -1e3))),]
plot(Trace.1 ~ Time, data = DF1)
fit <- nls(Trace.1 ~ SSasymp(Time, Asym, R0, lrc), data = DF1)
summary(fit)
coef(fit)
help("SSasymp") #for an explanation of the parameters
lines(DF1$Time, predict(fit))

Calculating Time Weighted Rate of Return in R

Is there an R function or library that will give me the monthly (or any other specified timeframe) time weighted rate of return (twrr) for my portfolio?
I am including a dput dump of sample data below of the date and portfolio ending balance below. Not sure why the dates were dput'ed the way they were, but the first date 12053 is '2003-01-01' and the last date 12195 is '2003-05-23'.
portfolio.df <- structure(
list(
Date = structure(c(12053, 12054, 12055, 12058,
12059, 12060, 12061, 12062, 12065, 12066, 12067, 12068, 12069,
12073, 12074, 12075, 12076, 12079, 12080, 12081, 12082, 12083,
12086, 12087, 12088, 12089, 12090, 12093, 12094, 12095, 12096,
12097, 12101, 12102, 12103, 12104, 12107, 12108, 12109, 12110,
12111, 12114, 12115, 12116, 12117, 12118, 12121, 12122, 12123,
12124, 12125, 12128, 12129, 12130, 12131, 12132, 12135, 12136,
12137, 12138, 12139, 12142, 12143, 12144, 12145, 12146, 12149,
12150, 12151, 12152, 12153, 12156, 12157, 12158, 12159, 12163,
12164, 12165, 12166, 12167, 12170, 12171, 12172, 12173, 12174,
12177, 12178, 12179, 12180, 12181, 12184, 12185, 12186, 12187,
12188, 12191, 12192, 12193, 12194, 12195),
class = "Date"),
Ending_Balance = c(56250000L,
56852500L, 57080000L, 57355000L, 57477500L, 56817500L, 57885000L,
57810000L, 57732500L, 57670000L, 57520000L, 57285000L, 57270000L,
56655000L, 55802500L, 56337500L, 55642500L, 54510000L, 54987500L,
55802500L, 56065000L, 56865000L, 56635000L, 56497500L, 56640000L,
56155000L, 55757500L, 55972500L, 55865000L, 55535000L, 55885000L,
56840000L, 56902500L, 56945000L, 56622500L, 57012500L, 57200000L,
58072500L, 57612500L, 57447500L, 57157500L, 57032500L, 57405000L,
57502500L, 56785000L, 57007500L, 56342500L, 55697500L, 56655000L,
56900000L, 57002500L, 57465000L, 57467500L, 57382500L, 57982500L,
56562500L, 58065000L, 58935000L, 58502500L, 58200000L, 57767500L,
57757500L, 58055000L, 58305000L, 58277500L, 58295000L, 59047500L,
58907500L, 59125000L, 59072500L, 59107500L, 59315000L, 59690000L,
58957500L, 59407500L, 59385000L, 59965000L, 60297500L, 59890000L,
59822500L, 60367500L, 60407500L, 60380000L, 60815000L, 61155000L,
61080000L, 61132500L, 61265000L, 60912500L, 61107500L, 61445000L,
61345000L, 61137500L, 61035000L, 60707500L, 61340000L, 61365000L,
61402500L, 61640000L, 61675000L)),
.Names = c("Date", "Ending_Balance"),
row.names = c(NA, 100L),
class = "data.frame")

Resources