Finding statistics after grouping in data.table - r

I had a small question in regards to data.table. Since i'm not so good at it i'm not quite sure how I can do this in data.table.
Basically I have 3 columns and want to group by the first two columns ( key and date ) and then for each key and each date, find the maximum and minimum that occurred in the third column ( fare)
I tried doing this but it gives me an error
flights[, c("max_day", "min_day") := unlist(lapply(gross_fare, findr)), by = c("key", "created_date")]
Error in `[.data.table`(flights, , `:=`(c("max_day", "min_day"), unlist(lapply(gross_fare, :
Supplied 18 items to be assigned to group 1 of size 9 in column 'max_day'. The RHS length must either be 1 (single values are ok) or match the LHS length exactly. If you wish to 'recycle' the RHS please use rep() explicitly to make this intent clear to readers of your code.
findr is a function which just finds the max and min i.e.
findr <- function(x) {list(max = max(x), min = min(x)}
I've done what I want to do in dplyr and I'll attach the code for that, but since i have millions of rows, dplyr eats up my ram so data.table would help
test <- flights %>%
select(key, created_date, gross_fare) %>%
group_by(key, created_date) %>%
summarise(
max_day = max(gross_fare),
min_day = min(gross_fare),
diff = max_day - min_day) %>%
arrange(created_date)
I've put the dput output if anyone wants to use that
If anyone can help that'd be great, thank you :)
data.table::setDT(structure(list(key = c("LHE_KHI_LHE+KHI_PA-405_15.0_1", "KHI_ISB_KHI+ISB_PK-370_20.0_0",
"LHE_KHI_LHE+KHI_PK-307_20.0_0", "ISB_KHI_ISB+KHI_PF-124_20.0_1",
"LHE_KHI_LHE+KHI_PK-307_20.0_0", "LHE_KHI_LHE+KHI_PA-405_15.0_1",
"KHI_LHE_KHI+LHE_PK-304_20.0_0", "KHI_ISB_KHI+ISB_PA-204_15.0_1",
"ISB_KHI_ISB+KHI_PA-207_15.0_1", "KHI_ISB_KHI+ISB_PA-200_20.0_1",
"KHI_LHE_KHI+LHE_PK-304_40.0_0", "ISB_KHI_ISB+KHI_PA-201_35.0_1",
"ISB_KHI_ISB+KHI_ER-501_20.0_1", "KHI_LHE_KHI+LHE_PF-145_20.0_2",
"KHI_ISB_KHI+ISB_PA-204_20.0_1", "LHE_KHI_LHE+KHI_PA-401_0.0_0",
"ISB_KHI_ISB+KHI_PK-309_40.0_0", "KHI_ISB_KHI+ISB_PF-123_20.0_2",
"ISB_KHI_ISB+KHI_PA-205_15.0_1", "LHE_KHI_LHE+KHI_PF-142_0.0_0",
"ISB_KHI_ISB+KHI_PA-223_15.0_1", "ISB_KHI_ISB+KHI_PF-126_20.0_2",
"ISB_KHI_ISB+KHI_PK-309_20.0_0", "KHI_ISB_KHI+ISB_PF-121_20.0_2",
"ISB_KHI_ISB+KHI_PK-373_20.0_0", "KHI_LHE_KHI+LHE_PF-145_20.0_2",
"KHI_LHE_KHI+LHE_PA-402_15.0_1", "LHE_KHI_LHE+KHI_PA-407_20.0_1",
"KHI_ISB_KHI+ISB_PK-308_40.0_0", "KHI_LHE_KHI+LHE_PF-145_20.0_2",
"LHE_KHI_LHE+KHI_PF-144_0.0_0", "ISB_KHI_ISB+KHI_PK-369_40.0_0",
"ISB_KHI_ISB+KHI_PF-124_20.0_2", "KHI_ISB_KHI+ISB_PA-204_15.0_1",
"KHI_ISB_KHI+ISB_PA-200_15.0_1", "ISB_KHI_ISB+KHI_PF-124_20.0_1",
"KHI_ISB_KHI+ISB_PK-300_20.0_0", "ISB_KHI_ISB+KHI_PF-122_20.0_2",
"KHI_ISB_KHI+ISB_PK-368_20.0_0", "KHI_ISB_KHI+ISB_PA-204_15.0_1",
"ISB_KHI_ISB+KHI_ER-503_20.0_1", "ISB_KHI_ISB+KHI_PA-209_15.0_1",
"KHI_ISB_KHI+ISB_PK-308_40.0_0", "ISB_KHI_ISB+KHI_PF-124_20.0_1",
"ISB_KHI_ISB+KHI_PK-301_40.0_0", "KHI_LHE_KHI+LHE_PA-408_35.0_1",
"LHE_KHI_LHE+KHI_PF-144_20.0_2", "KHI_ISB_KHI+ISB_PF-121_20.0_2",
"KHI_ISB_KHI+ISB_PA-204_35.0_1", "ISB_KHI_ISB+KHI_PK-309_40.0_0",
"ISB_KHI_ISB+KHI_PA-223_20.0_1", "KHI_ISB_KHI+ISB_PA-206_35.0_1",
"LHE_KHI_LHE+KHI_PF-142_32.0_1", "LHE_KHI_LHE+KHI_PF-142_20.0_1",
"KHI_ISB_KHI+ISB_PF-123_20.0_2", "ISB_KHI_ISB+KHI_PA-209_15.0_1",
"KHI_ISB_KHI+ISB_PA-204_35.0_1", "ISB_KHI_ISB+KHI_PA-201_20.0_1",
"KHI_ISB_KHI+ISB_PK-368_20.0_0", "ISB_KHI_ISB+KHI_PA-205_20.0_1",
"KHI_ISB_KHI+ISB_PF-121_20.0_1", "ISB_KHI_ISB+KHI_PF-124_20.0_1",
"ISB_KHI_ISB+KHI_PA-205_15.0_1", "KHI_LHE_KHI+LHE_PF-145_20.0_2",
"KHI_LHE_KHI+LHE_PA-406_35.0_1", "KHI_ISB_KHI+ISB_PK-308_20.0_0",
"LHE_KHI_LHE+KHI_PA-401_20.0_1", "LHE_KHI_LHE+KHI_PA-401_15.0_1",
"KHI_ISB_KHI+ISB_PA-204_35.0_1", "KHI_LHE_KHI+LHE_PA-406_35.0_1",
"KHI_ISB_KHI+ISB_PA-206_35.0_1", "KHI_ISB_KHI+ISB_PF-121_20.0_1",
"ISB_KHI_ISB+KHI_PA-205_20.0_1", "LHE_KHI_LHE+KHI_PF-142_20.0_1",
"LHE_KHI_LHE+KHI_PF-146_20.0_2", "LHE_KHI_LHE+KHI_PA-401_35.0_1",
"ISB_KHI_ISB+KHI_PA-209_15.0_1", "ISB_KHI_ISB+KHI_PK-301_40.0_0",
"ISB_KHI_ISB+KHI_PA-205_35.0_1", "KHI_LHE_KHI+LHE_PA-406_15.0_1",
"KHI_ISB_KHI+ISB_PF-123_20.0_1", "ISB_KHI_ISB+KHI_PA-201_35.0_1",
"KHI_ISB_KHI+ISB_PK-300_40.0_0", "KHI_LHE_KHI+LHE_PA-402_35.0_1",
"ISB_KHI_ISB+KHI_ER-505_20.0_1", "ISB_KHI_ISB+KHI_PF-122_20.0_2",
"ISB_KHI_ISB+KHI_PA-207_15.0_1", "KHI_LHE_KHI+LHE_PA-404_35.0_1",
"KHI_ISB_KHI+ISB_PF-123_20.0_1", "ISB_KHI_ISB+KHI_ER-503_20.0_1",
"ISB_GIL_ISB+GIL_PK-605_20.0_0", "KHI_ISB_KHI+ISB_PF-123_20.0_1",
"KHI_ISB_KHI+ISB_PA-200_15.0_1", "ISB_KHI_ISB+KHI_PF-122_20.0_2",
"KHI_LHE_KHI+LHE_PA-404_35.0_1", "ISB_KHI_ISB+KHI_PF-122_20.0_2",
"PEW_KHI_PEW+KHI_PF-152_20.0_1", "LHE_KHI_LHE+KHI_PK-303_20.0_0",
"KHI_ISB_KHI+ISB_PA-222_35.0_1", "ISB_KHI_ISB+KHI_PF-124_20.0_1"
), created_date = c("2021-04-20", "2021-05-27", "2021-02-13",
"2021-08-14", "2021-08-11", "2021-08-21", "2021-01-26", "2021-08-21",
"2021-05-24", "2021-09-15", "2021-06-05", "2021-07-19", "2021-09-29",
"2021-07-02", "2021-08-10", "2021-01-04", "2021-07-15", "2021-07-14",
"2021-08-13", "2021-01-11", "2021-09-13", "2021-09-20", "2021-05-27",
"2021-02-20", "2021-08-15", "2021-07-27", "2021-08-26", "2021-09-15",
"2021-08-02", "2021-06-25", "2021-05-15", "2021-08-26", "2021-07-30",
"2021-06-27", "2021-08-07", "2021-03-19", "2021-03-02", "2021-06-06",
"2021-08-15", "2021-06-27", "2021-09-19", "2021-07-28", "2021-08-09",
"2021-08-16", "2021-09-09", "2021-06-04", "2021-08-12", "2021-05-15",
"2021-07-26", "2021-05-27", "2021-08-12", "2021-08-02", "2021-01-26",
"2021-04-20", "2021-08-26", "2021-08-26", "2021-03-21", "2021-01-09",
"2021-04-23", "2021-01-04", "2021-08-13", "2021-06-22", "2021-05-31",
"2021-08-18", "2021-06-16", "2021-08-14", "2021-08-10", "2021-06-16",
"2021-04-08", "2021-05-20", "2021-06-22", "2021-04-20", "2021-01-05",
"2021-02-27", "2021-07-07", "2021-03-26", "2021-08-16", "2021-05-01",
"2021-07-31", "2021-06-14", "2021-06-16", "2021-03-25", "2021-09-14",
"2021-06-06", "2021-09-02", "2021-08-06", "2021-07-18", "2021-02-28",
"2021-04-28", "2021-09-19", "2021-08-25", "2021-06-17", "2021-06-07",
"2021-06-17", "2021-07-07", "2021-08-23", "2021-07-09", "2021-07-19",
"2021-07-14", "2021-05-21"), gross_fare = c(7796, 7427, 11504,
6870, 6580, 14945, 8697, 7524, 7124, 6785, 11858, 7524, 11500,
9525, 6785, 8739, 8200, 13560, 9045, 7400, 7524, 12500, 7458,
14000, 6570, 9525, 6220, 10545, 8310, 7900, 7820, 8410, 11285,
19892, 6810, 9800, 11441, 11900, 6570, 13592, 11500, 8300, 20380,
8525, 7340, 9707, 7870, 10655, 10545, 11798, 14645, 10545, 8650,
8650, 7870, 12945, 10799, 10227, 6765, 10227, 20120, 11045, 9403,
7870, 7124, 6570, 6810, 6531, 8605, 7124, 11072, 7390, 10227,
13435, 10530, 12280, 18945, 11147, 10545, 6531, 6620, 10799,
18480, 32702, 5606, 13560, 23895, 8027, 9655, 11500, 11990, 6620,
9403, 7620, 14645, 19105, 9000, 6440, 12645, 8025)), row.names = c(NA,
-100L), class = c("data.table", "data.frame")))

I guess this line of code should do the job:
library(data.table)
flights[, .(min_day = min(gross_fare), max_day = max(gross_fare), diff = max(gross_fare) - min(gross_fare)), by = .(key, created_date)][]

Since the function findr returns a list, there's no need to complicate things:
findr <- function(x) {list(max = max(x), min = min(x))}
flights[, c("max_day", "min_day") := findr(gross_fare), by = list(key, created_date)][]
To also return the difference between max and min, use
findr2 <- function(x) {
list(max = max(x), min = min(x), diff = diff(range(x)))
}
flights[, c("max_day", "min_day", "diff_day") := findr2(gross_fare), by = list(key, created_date)][]

Related

Plot time series without straight lines

I am trying to plot some time series but since I have data only for the summer I get these straight lines. Any idea how to fix that? The code I used: Any idea would be helpful!
ggplot(ba, aes(x=date1, y=pc1)) +
geom_line(color="turquoise4") +
theme_minimal() +
labs(x="", y="Loading", title="Correlation of PC1 and original series") +
theme(plot.title = element_text(hjust=0.5, size=20, face="bold"))+
scale_x_date(date_labels= ("%Y"))
And the plot:
The data: structure(list(date1 = structure(c(10712, 10713, 10714, 10715,
10716, 10717, 10718, 10719, 10720, 10721, 10722, 10723, 10724,
10725, 10726, 10727, 10728, 10729, 10730, 10731, 10732, 10733,
10734, 10735, 10736, 10737, 10738, 10739, 10740, 10741, 10742,
10743, 10744, 10745, 10746, 10747, 10748, 10749, 10750, 10751,
10752, 10753, 10754, 10755, 10756, 10757, 10758, 10759, 10760,
10761, 10762, 10763, 10764, 10765, 10766, 10767, 10768, 10769,
10770, 10771, 10772, 10773, 10774, 10775, 10776, 10777, 10778,
10779, 10780, 10781, 10782, 10783, 10784, 10785, 10786, 10787,
10788, 10789, 10790, 10791, 10792, 10793, 10794, 10795, 10796,
10797, 10798, 10799, 10800, 10801, 10802, 10803, 10804, 10805,
10806, 10807, 10808, 10809, 10810, 10811, 10812, 10813, 10814,
10815, 10816, 10817, 10818, 10819, 10820, 10821, 10822, 10823,
10824, 10825, 10826, 10827, 10828, 10829, 10830, 10831, 10832,
10833, 10834, 10835, 10836, 10837, 10838, 10839, 10840, 10841,
10842, 10843, 10844, 10845, 10846, 10847, 10848, 10849, 10850,
10851, 10852, 10853, 10854, 10855, 10856, 10857, 10858, 10859,
10860, 10861, 10862, 10863, 10864, 11078, 11079, 11080, 11081,
11082, 11083, 11084, 11085, 11086, 11087, 11088, 11089, 11090,
11091, 11092, 11093, 11094, 11095, 11096, 11097, 11098, 11099,
11100, 11101, 11102, 11103, 11104, 11105, 11106, 11107, 11108,
11109, 11110, 11111, 11112, 11113, 11114, 11115, 11116, 11117,
11118, 11119, 11120, 11121, 11122, 11123, 11124), class = "Date"),
pc1 = c(2.64462123197862, 2.4380313244096, 2.21417935009087,
2.02249236956036, 1.75829175459456, 1.58770371446918, 1.62230139615394,
1.73502227021784, 1.75083678213192, 1.64509065138032, 1.57921033180313,
1.70228767677341, 1.77303175099386, 1.78384290706931, 1.86580160595479,
1.9106874120324, 1.73936455049801, 1.50577059168685, 1.24226003967481,
1.07813468676617, 1.06276891964951, 1.09622663209529, 1.07692457712675,
0.978692818737612, 1.06365064520783, 1.2525349982313, 1.08237838015766,
0.645239033194787, 0.479482241789711, 0.683701830568681,
0.792197472275541, 0.631531270886538, 0.520337262457156,
0.667200695099021, 0.767559380073353, 0.7856163663635, 0.737745147101418,
0.654712633988225, 0.440140874164089, 0.111631055132755,
-0.22450806112272, -0.444238159039355, -0.584576558346287,
-0.444097467542865, -0.227821057355029, -0.120935149111578,
-0.0932195161137341, 0.037283855810637, 0.206479031035409,
0.173515424607062, 0.234536409515456, 0.317957256707112,
0.290090191780606, 0.0607339338833623, -0.27556992053308,
-0.3586166955826, -0.3534130521313, -0.501651666926942, -0.571570071652576,
-0.79110428934397, -0.985635595643097, -0.994138228085185,
-0.839909782593256, -0.699274458194957, -0.580683825031177,
-0.530811870371419, -0.4746353951302, -0.489386570992314,
-0.787222651887671, -1.1059671054324, -1.17983265148469,
-1.1058432515423, -0.970485807735322, -0.679713450749357,
-0.516950863200668, -0.495312393712548, -0.673645368786615,
-0.792675131421433, -0.692021409445821, -0.611096320716252,
-0.676712376641795, -0.723244566814595, -0.621986199057006,
-0.563969216349158, -0.649311354664407, -0.679237194242732,
-0.624476984795223, -0.738344795218295, -0.877867797047079,
-0.879375052767018, -0.84262582765393, -0.845707036138972,
-0.959691974084994, -1.06904324062176, -0.97905489332525,
-0.847145240762566, -0.86837819324592, -0.935323976060101,
-0.796486491787169, -0.461073031709012, -0.275818888900351,
-0.513613296467615, -0.786611502858454, -0.799843667083875,
-0.632676241199403, -0.468611824279096, -0.534017599627378,
-0.501551518704511, -0.239313348556757, -0.208935210151003,
-0.510483950549102, -0.62974750963569, -0.399113422985878,
-0.072812659658845, 0.0377885597304766, -0.0102829082610216,
-0.0571349366394233, -0.101917027852624, -0.202941574141862,
-0.22849727264844, -0.125157862652187, 0.168703915373856,
0.43626132948925, 0.446099489882147, 0.435379929023588, 0.236210503991287,
-0.122289033919648, -0.288101855449495, -0.186400543130663,
0.0316721901308679, 0.121240481805255, -0.0753698973566349,
-0.384779730900963, -0.531179497125517, -0.373632181420806,
-0.0148926315001478, 0.146040939981569, 0.13371186468668,
0.200262938351445, 0.465073170745138, 0.506805629621484,
0.345398737766814, 0.171110245173291, 0.176555396235594,
0.262743070740985, 0.398601589660576, 0.433248104072272,
0.453883432665361, 0.604637145172226, 0.843278371818699,
1.13506306230201, 1.42652005730684, 1.63221068108998, 1.86442509826484,
1.97067279998339, 2.0139860665512, 2.13720187260212, 2.31355711206366,
2.32477728002809, 2.36236228869303, 2.24108767618426, 2.12991693141636,
2.11677885248848, 2.01466853738993, 1.77967782944265, 1.48938981000699,
1.34042958586002, 1.33016846412245, 1.31770813627339, 1.26104969519401,
1.37385446004522, 1.61517275597383, 1.84510291043685, 1.91280500843462,
1.84897419443657, 1.52674793906846, 1.29429812528379, 1.06717755247561,
0.910500917679731, 0.904461327314293, 1.05380123048097, 1.08631739987863,
1.04843964584885, 1.10153891962662, 1.15936307711726, 1.20129772010444,
1.18746954945955, 1.00056619329093, 0.725225823060771, 0.573790799694267,
0.655776789864271, 0.780033607405981, 0.664875593837605,
0.452000300833336, 0.394589410057676, 0.402170545544567,
0.403979206396259, 0.395485848597801, 0.433314713756909,
0.437960603442615)), row.names = c(NA, 200L), class = "data.frame")strong text
1) Using the input in the Note at the end expand the dates to include the missing ones using NA's for them. Then plot.
library(ggplot2)
library(zoo)
z <- read.zoo(ba)
zz <- merge(z, zoo(, seq(start(z), end(z), 1)))
autoplot(zz) + xlab("")
2) Another approach is to use distinct facets for each year.
library(ggplot2)
breaks <- unique(as.Date(cut(ba$date1, "month")))
ba2 <- transform(ba, year = as.integer(format(date1, "%Y")))
p <- ggplot(ba2, aes(date1, pc1)) +
geom_line() +
facet_grid(cols = vars(year), scales = "free_x", space = "free_x")
p + scale_x_date(breaks = breaks, date_labels = "%b")
(continued after image)
or to remove strip text and only place the year below each facet use p from above with
breaks <- as.Date(tapply(format(ba$date1), format(ba$date1, "%Y"), min))
p +
scale_x_date(breaks = breaks, date_labels = "%Y") +
theme(strip.text.x = element_blank())
3) The facet idea could also be implemented in lattice.
library(lattice)
ba3 <- transform(ba, year = format(date1, "%Y"))
xyplot(pc1 ~ date1 | year, ba3, type = "l",
scales = list(x = list(relation = "free")), layout = c(NA, 1))
Note
There was a problem with the dput output in the question so the following was used.
ba <-
structure(list(date1 = structure(c(10712, 10713, 10714, 10715,
10716, 10717, 10718, 10719, 10720, 10721, 10722, 10723, 10724,
10725, 10726, 10727, 10728, 10729, 10730, 10731, 10732, 10733,
10734, 10735, 10736, 10737, 10738, 10739, 10740, 10741, 10742,
10743, 10744, 10745, 10746, 10747, 10748, 10749, 10750, 10751,
10752, 10753, 10754, 10755, 10756, 10757, 10758, 10759, 10760,
10761, 10762, 10763, 10764, 10765, 10766, 10767, 10768, 10769,
10770, 10771, 10772, 10773, 10774, 10775, 10776, 10777, 10778,
10779, 10780, 10781, 10782, 10783, 10784, 10785, 10786, 10787,
10788, 10789, 10790, 10791, 10792, 10793, 10794, 10795, 10796,
10797, 10798, 10799, 10800, 10801, 10802, 10803, 10804, 10805,
10806, 10807, 10808, 10809, 10810, 10811, 10812, 10813, 10814,
10815, 10816, 10817, 10818, 10819, 10820, 10821, 10822, 10823,
10824, 10825, 10826, 10827, 10828, 10829, 10830, 10831, 10832,
10833, 10834, 10835, 10836, 10837, 10838, 10839, 10840, 10841,
10842, 10843, 10844, 10845, 10846, 10847, 10848, 10849, 10850,
10851, 10852, 10853, 10854, 10855, 10856, 10857, 10858, 10859,
10860, 10861, 10862, 10863, 10864, 11078, 11079, 11080, 11081,
11082, 11083, 11084, 11085, 11086, 11087, 11088, 11089, 11090,
11091, 11092, 11093, 11094, 11095, 11096, 11097, 11098, 11099,
11100, 11101, 11102, 11103, 11104, 11105, 11106, 11107, 11108,
11109, 11110, 11111, 11112, 11113, 11114, 11115, 11116, 11117,
11118, 11119, 11120, 11121, 11122, 11123, 11124), class = "Date"),
pc1 = c(2.64462123197862, 2.4380313244096, 2.21417935009087,
2.02249236956036, 1.75829175459456, 1.58770371446918, 1.62230139615394,
1.73502227021784, 1.75083678213192, 1.64509065138032, 1.57921033180313,
1.70228767677341, 1.77303175099386, 1.78384290706931, 1.86580160595479,
1.9106874120324, 1.73936455049801, 1.50577059168685, 1.24226003967481,
1.07813468676617, 1.06276891964951, 1.09622663209529, 1.07692457712675,
0.978692818737612, 1.06365064520783, 1.2525349982313, 1.08237838015766,
0.645239033194787, 0.479482241789711, 0.683701830568681,
0.792197472275541, 0.631531270886538, 0.520337262457156,
0.667200695099021, 0.767559380073353, 0.7856163663635, 0.737745147101418,
0.654712633988225, 0.440140874164089, 0.111631055132755,
-0.22450806112272, -0.444238159039355, -0.584576558346287,
-0.444097467542865, -0.227821057355029, -0.120935149111578,
-0.0932195161137341, 0.037283855810637, 0.206479031035409,
0.173515424607062, 0.234536409515456, 0.317957256707112,
0.290090191780606, 0.0607339338833623, -0.27556992053308,
-0.3586166955826, -0.3534130521313, -0.501651666926942, -0.571570071652576,
-0.79110428934397, -0.985635595643097, -0.994138228085185,
-0.839909782593256, -0.699274458194957, -0.580683825031177,
-0.530811870371419, -0.4746353951302, -0.489386570992314,
-0.787222651887671, -1.1059671054324, -1.17983265148469,
-1.1058432515423, -0.970485807735322, -0.679713450749357,
-0.516950863200668, -0.495312393712548, -0.673645368786615,
-0.792675131421433, -0.692021409445821, -0.611096320716252,
-0.676712376641795, -0.723244566814595, -0.621986199057006,
-0.563969216349158, -0.649311354664407, -0.679237194242732,
-0.624476984795223, -0.738344795218295, -0.877867797047079,
-0.879375052767018, -0.84262582765393, -0.845707036138972,
-0.959691974084994, -1.06904324062176, -0.97905489332525,
-0.847145240762566, -0.86837819324592, -0.935323976060101,
-0.796486491787169, -0.461073031709012, -0.275818888900351,
-0.513613296467615, -0.786611502858454, -0.799843667083875,
-0.632676241199403, -0.468611824279096, -0.534017599627378,
-0.501551518704511, -0.239313348556757, -0.208935210151003,
-0.510483950549102, -0.62974750963569, -0.399113422985878,
-0.072812659658845, 0.0377885597304766, -0.0102829082610216,
-0.0571349366394233, -0.101917027852624, -0.202941574141862,
-0.22849727264844, -0.125157862652187, 0.168703915373856,
0.43626132948925, 0.446099489882147, 0.435379929023588, 0.236210503991287,
-0.122289033919648, -0.288101855449495, -0.186400543130663,
0.0316721901308679, 0.121240481805255, -0.0753698973566349,
-0.384779730900963, -0.531179497125517, -0.373632181420806,
-0.0148926315001478, 0.146040939981569, 0.13371186468668,
0.200262938351445, 0.465073170745138, 0.506805629621484,
0.345398737766814, 0.171110245173291, 0.176555396235594,
0.262743070740985, 0.398601589660576, 0.433248104072272,
0.453883432665361, 0.604637145172226, 0.843278371818699,
1.13506306230201, 1.42652005730684, 1.63221068108998, 1.86442509826484,
1.97067279998339, 2.0139860665512, 2.13720187260212, 2.31355711206366,
2.32477728002809, 2.36236228869303, 2.24108767618426, 2.12991693141636,
2.11677885248848, 2.01466853738993, 1.77967782944265, 1.48938981000699,
1.34042958586002, 1.33016846412245, 1.31770813627339, 1.26104969519401,
1.37385446004522, 1.61517275597383, 1.84510291043685, 1.91280500843462,
1.84897419443657, 1.52674793906846, 1.29429812528379, 1.06717755247561,
0.910500917679731, 0.904461327314293, 1.05380123048097, 1.08631739987863,
1.04843964584885, 1.10153891962662, 1.15936307711726, 1.20129772010444,
1.18746954945955, 1.00056619329093, 0.725225823060771, 0.573790799694267,
0.655776789864271, 0.780033607405981, 0.664875593837605,
0.452000300833336, 0.394589410057676, 0.402170545544567,
0.403979206396259, 0.395485848597801, 0.433314713756909,
0.437960603442615)), row.names = c(NA, -200L), class = "data.frame")

Saving output of splice() to a text file

I have the following script
library(zoo)
library(dplyr)
library(tidyverse)
dat<-read.csv("sep_2015.csv",header=F)
dat$date<-seq(from=as.Date("2015-09-01"),to=as.Date("2015-12-31"),by="day")
dat %>%
rownames_to_column() %>%
filter(V2 < 90 &
rollsum(V2 < 90, 7, fill = NA, align = "left") >=7 &
rollsum(V1 > 2, 7, fill = NA, align = "left") >=7 &
rollsum(V2 < 90, 20, fill = NA, align = "left") >= 15 ) %>%
slice(1)
The output is like this:
rowname V1 V2 date
1 41 5.583102 45.83082 2015-10-11
I am processing multiple files (from sep_1979 to sep_2020) with the following names:
.....
sep_2014.csv
sep_2015.csv
sep_2016.csv
....
Sample data (sep_2015.csv):
structure(list(V1 = c(1.781016934, 5.245650757, 5.253808428,
3.4500901, 1.904631502, 5.583155269, 5.29432007, 2.493240863,
2.356109739, 2.505279709, 3.100681377, 4.352358556, 9.021734968,
7.037204701, 1.946181317, 2.375415753, 4.818568252, 4.508796957,
5.009930139, 3.413858254, 2.642863447, 4.496943406, 1.101138556,
1.58130412, 5.907897257, 9.310602975, 12.8050039, 10.52127595,
10.58324383, 5.719938811, 5.65915423, 10.23184856, 8.916484733,
8.006767597, 7.623137477, 6.735120637, 3.913288259, 5.145787145,
5.516434408, 3.766181753, 5.583101736, 8.62718523, 8.55994889,
7.738160311, 9.428281922, 14.54588945, 21.1567956, 19.25095128,
11.18693546, 6.459297562, 8.113929381, 6.791538854, 2.30569946,
4.271416627, 7.043436661, 4.044279911, 4.841727481, 7.050044333,
8.646260752, 4.475859911, 9.545056312, 6.185563838, 7.545238783,
8.397502556, 6.124560982, 4.50970303, 6.682802556, 6.247809616,
5.986996325, 4.525555447, 8.719674593, 8.245588214, 6.028287485,
2.346912866, 2.842578055, 7.320520815, 6.337556706, 3.298882387,
4.512427524, 8.360934487, 9.222761141, 8.958611924, 7.982833144,
10.50259016, 8.866081434, 5.120412581, 11.46034903, 12.4205173,
10.25257175, 2.466221616, 2.692953769, 0.681709806, 1.362974813,
5.5663453, 9.601658711, 8.416295206, 3.412426219, 6.865540765,
11.76102145, 12.0902957, 6.870356614, 2.020952899, 4.363628479,
4.879838354, 4.059975788, 10.37719133, 10.37127041, 11.40995727,
10.7018141, 11.43079722, 6.58367129, 6.628593068, 2.18586382,
3.712579965, 2.874565445, 2.791741886, 4.21014224, 0.881548513,
8.251492289, 7.62408847, 5.940576571, 8.981119084), V2 =
c(121.0261069,
85.00304523, 81.87989778, 103.7505395, 210.2369147, 264.2405565,
282.9403406, 26.88123501, 19.2594093, 8.196100414, 45.26456729,
58.17439097, 87.16714037, 109.3834486, 114.840361, 38.91351028,
37.81463493, 24.49148024, 29.33726467, 13.51886983, 78.0736854,
41.91203091, 55.80655486, 338.9064404, 338.2241727, 4.297849857,
343.7324297, 266.8110122, 189.2300192, 118.7736246, 86.03606596,
120.0474015, 151.7655159, 85.86331853, 67.23147222, 51.7641341,
20.78357193, 271.0026858, 266.308435, 311.3070459, 45.83082398,
64.93928317, 63.04855403, 35.94151677, 43.73557838, 55.52061899,
55.9783368, 72.09428879, 81.11278896, 75.28379332, 56.17839757,
46.85929244, 27.56930947, 64.41308939, 75.61366497, 66.62037577,
43.60322015, 89.88800444, 101.8289993, 99.75479947, 80.43658344,
78.52117729, 83.81080108, 91.30824192, 96.42780607, 101.3614562,
118.3864844, 101.9711437, 103.8823967, 83.42613393, 74.27778556,
97.92986085, 113.0191678, 147.4666885, 134.7889272, 104.79725,
126.037476, 121.3500898, 83.21475735, 89.23421897, 92.92463803,
85.92837336, 80.02208464, 55.23356116, 52.95700516, 30.38190174,
64.76557575, 96.63946752, 86.73461083, 95.37929333, 62.17987768,
12.7633562, 273.7881121, 76.47255644, 113.7227032, 149.0157726,
78.67676081, 71.17172364, 89.33577561, 154.9133108, 221.6713695,
85.69030561, 78.51796584, 97.6857188, 78.00024078, 67.04777272,
66.22047934, 66.65994223, 68.62200021, 79.88214929, 84.2911525,
98.45874358, 175.7545009, 214.3253056, 277.4996992, 348.4682651,
97.83108644, 348.9214276, 49.4694104, 70.54130568, 79.26767621,
80.66615013), date = structure(c(16679, 16680, 16681, 16682,
16683, 16684, 16685, 16686, 16687, 16688, 16689, 16690, 16691,
16692, 16693, 16694, 16695, 16696, 16697, 16698, 16699, 16700,
16701, 16702, 16703, 16704, 16705, 16706, 16707, 16708, 16709,
16710, 16711, 16712, 16713, 16714, 16715, 16716, 16717, 16718,
16719, 16720, 16721, 16722, 16723, 16724, 16725, 16726, 16727,
16728, 16729, 16730, 16731, 16732, 16733, 16734, 16735, 16736,
16737, 16738, 16739, 16740, 16741, 16742, 16743, 16744, 16745,
16746, 16747, 16748, 16749, 16750, 16751, 16752, 16753, 16754,
16755, 16756, 16757, 16758, 16759, 16760, 16761, 16762, 16763,
16764, 16765, 16766, 16767, 16768, 16769, 16770, 16771, 16772,
16773, 16774, 16775, 16776, 16777, 16778, 16779, 16780, 16781,
16782, 16783, 16784, 16785, 16786, 16787, 16788, 16789, 16790,
16791, 16792, 16793, 16794, 16795, 16796, 16797, 16798, 16799,
16800), class = "Date")), row.names = c(NA, -122L), class = "data.frame")
I would like to:
(1) apply the above script for each file
(2) save the "date" from splice (1) into a text file
(3) the final output is a collection of dates from 1979 to 2020
How can I implement this in R? I'll appreciate any help on this.
You can try the following solution :
From each file name extract the year value.
Create sequence of dates for that year using paste and add it as a column in the data.
Filter the rows for the condition and select the 1st row from them.
Combine the result into one file.
library(tidyverse)
library(zoo)
all_files <- list.files(pattern = '^sep_.*\\.csv$')
map_df(all_files, function(file) {
year <- parse_number(file)
dates <- seq(as.Date(paste(year, 9, 1, sep = '-')),
as.Date(paste(year, 12, 31, sep = '-')), by = 'day')
dat <- read.csv(file,header=FALSE)
dat %>%
mutate(date = dates) %>%
filter(V2 < 90 &
rollsum(V2 < 90, 7, fill = NA, align = "left") >=7 &
rollsum(V1 > 2, 7, fill = NA, align = "left") >=7 &
rollsum(V2 < 90, 20, fill = NA, align = "left") >= 15) %>%
slice(1) %>%
transmute(date, file = file)
}) -> result
result should look something like this -
result
# date filename
#1979-10-10 sep_1979.csv
#1980-11-21 sep_1980.csv
#1981-09-10 sep_1981.csv
#
#
You can then write result into csv or text file as per your choice.
#CSV
write.csv(result, 'result.csv', row.names = FALSE)
#TEXT
write.table(result, 'result.txt', row.names = FALSE)

Add a 3d surface at point 0 in plotly

I have a 3D plot using plotly which can take on negative and positive values. I would like to add a see through 3D flat surface at point 0 (see through here means setting alpha = 0.3 as in ggplot2) in order to emphasize the positive/negative values a little more. Alternatively adding a "grid" as in the background through the point 0 would be useful.
Code:
library(plotly)
library(dplyr)
df %>%
select(date, everything()) %>%
plot_ly(
x = colnames(subset(., select = c(2:4))), # probably not the most efficient method
y = ~date,
z = data.matrix(subset(., select = c(2:4))),
type = "surface",
colors = c("darkblue", "yellow", "darkred")
)
Data:
df <- structure(list(wind = c(0.938535690307617, 0.976551234722137,
0.954551994800568, 0.920722126960754, 0.889751732349396, 0.701366603374481,
0.718878328800201, 0.685763895511627, 0.677822828292847, 0.876205325126648,
0.67054146528244, 0.738650739192963, 0.725420415401459, 0.851324200630188,
0.6589714884758, 0.880357265472412, 0.997677683830261, 0.968335390090942,
0.882899045944214, 1.09453165531158, 1.06842839717865, 0.819347560405731,
0.995919525623322, 0.695173263549805, 0.860199570655823, 0.977508246898651,
0.807886302471161, 0.778182446956635, 0.886279463768005, 0.879809498786926,
0.903579652309418, 1.09579062461853, 1.03768181800842, 0.802022874355316,
0.928451955318451, 0.744936227798462, 0.621560990810394, 0.706887602806091,
0.749234974384308, 0.746754884719849, 0.65381270647049, 0.710927248001099,
0.517793655395508, 0.477172255516052, 0.631662607192993, 0.55011123418808,
0.584086775779724, 0.617783904075623, 0.438043504953384, 0.577566087245941,
0.684398949146271, 1.00848543643951, 0.957233726978302, 0.84071296453476,
0.90149587392807, 0.888661623001099, 0.923080563545227, 0.945727407932281,
0.965020060539246, 1.07903909683228, 0.870955109596252, 0.934546709060669,
0.93314516544342, 0.926109850406647, 0.981102645397186, 0.770903468132019,
0.829349219799042, 0.980291783809662, 1.00809383392334, 0.821328639984131,
0.89130437374115, 0.886775732040405, 0.896964132785797, 1.08184945583344,
1.17395043373108, 1.11537420749664, 1.15379846096039, 1.20203125476837,
1.12583827972412, 1.10014677047729, 0.891100168228149, 1.01484513282776,
1.01270127296448, 0.895487844944, 0.877909421920776, 1.11780989170074,
1.18047833442688, 1.14531397819519, 1.28634309768677, 1.23702371120453,
1.01186645030975, 1.15563869476318, 1.0918824672699, 1.2023059129715,
1.11806273460388, 1.11690294742584, 1.13390302658081, 1.10064888000488,
1.1418149471283, 1.06309700012207), holiday = c(-0.323977619409561,
-0.32814821600914, -0.335260361433029, -0.355545252561569, -0.347469061613083,
-0.349574476480484, -0.331554502248764, -0.351303607225418, -0.342486947774887,
-0.34791961312294, -0.342795491218567, -0.344868391752243, -0.344075173139572,
-0.342763870954514, -0.365125387907028, -0.301046937704086, -0.303365021944046,
-0.301353365182877, -0.316689401865005, -0.312132269144058, -0.317412197589874,
-0.331865310668945, -0.306569844484329, -0.306523144245148, -0.328399240970612,
-0.328934848308563, -0.340233236551285, -0.310919582843781, -0.352815061807632,
-0.324335247278214, -0.333071410655975, -0.25605234503746, -0.28141376376152,
-0.267955660820007, -0.261935144662857, -0.23365119099617, -0.204256281256676,
-0.271868377923965, -0.266900211572647, -0.268673747777939, -0.246791422367096,
-0.261210560798645, -0.275096118450165, -0.272033154964447, -0.307673662900925,
-0.316463977098465, -0.315838783979416, -0.278884381055832, -0.29575651884079,
-0.300951153039932, -0.275152236223221, -0.292507320642471, -0.283683449029922,
-0.333169460296631, -0.323910266160965, -0.317758291959763, -0.318425863981247,
-0.318803340196609, -0.316879868507385, -0.321441829204559, -0.342198520898819,
-0.336640536785126, -0.334549427032471, -0.304668426513672, -0.281678169965744,
-0.287939876317978, -0.278673946857452, -0.299236595630646, -0.295697629451752,
-0.297617554664612, -0.300621718168259, -0.294943511486053, -0.291149199008942,
-0.263380706310272, -0.264828890562057, -0.260132044553757, -0.294039487838745,
-0.284087061882019, -0.27564924955368, -0.275646597146988, -0.294898957014084,
-0.290615618228912, -0.264040410518646, -0.267250239849091, -0.27454400062561,
-0.22650308907032, -0.220207497477531, -0.233633011579514, -0.205283910036087,
-0.201104089617729, -0.235431581735611, -0.252072185277939, -0.257701843976974,
-0.233586445450783, -0.239779070019722, -0.233227252960205, -0.26639312505722,
-0.256356805562973, -0.248022571206093, -0.285306513309479),
month_10 = c(0.109010718762875, 0.112969301640987, 0.110368527472019,
0.107052445411682, 0.0947703272104263, 0.109930463135242,
0.106371931731701, 0.114236004650593, 0.102411419153214,
0.0184143912047148, 0.0301767271012068, 0.0376978516578674,
0.0472327470779419, 0.0622735135257244, 0.043902475386858,
0.0611664243042469, 0.0618763938546181, 0.0555795393884182,
0.0623081848025322, 0.0636096075177193, 0.0675770491361618,
0.0629641935229301, 0.0288578178733587, 0.025121470913291,
0.0300221722573042, 0.0611664243042469, 0.0659252777695656,
0.0159557648003101, 0.0376978516578674, 0.032174538820982,
0.0317839048802853, 0.0626140907406807, 0.0577763170003891,
0.0571180321276188, 0.0571180321276188, 0.0590739175677299,
0.0760120898485184, 0.0670360922813416, 0.0699110627174377,
0.0717969089746475, 0.0640148967504501, 0.0670360922813416,
0.164964601397514, 0.152724280953407, 0.115071900188923,
0.129654854536057, 0.122471310198307, 0.0768138542771339,
0.0400041155517101, 0.0573667995631695, 0.0685276389122009,
0.0337212830781937, 0.0616994015872478, 0.0589617975056171,
0.059433214366436, 0.0567467175424099, 0.0661386772990227,
0.0704409778118134, 0.0611028559505939, 0.0579087659716606,
0.0622764676809311, 0.0538184903562069, 0.0553129874169827,
0.058912742882967, 0.059433214366436, 0.0575473643839359,
0.0575473643839359, 0.058104183524847, 0.055678017437458,
0.0575473643839359, 0.059433214366436, 0.0603105537593365,
0.0589617975056171, 0.0601625964045525, 0.0577935017645359,
0.0574705749750137, 0.0550192892551422, 0.0475285314023495,
0.0577935017645359, 0.0543489865958691, 0.0526755712926388,
0.0553129874169827, 0.0589617975056171, 0.0593133755028248,
0.0547068528831005, 0.0676216259598732, 0.0511428378522396,
0.0564789660274982, 0.0641900449991226, 0.0666491389274597,
0.0390096306800842, 0.058005329221487, 0.0449355207383633,
0.062143836170435, 0.0591179206967354, 0.0637796893715858,
0.0633300691843033, 0.0702810436487198, 0.0647093132138252,
0.0427459664642811), month_12 = c(0.0733551606535912, 0.0707022771239281,
0.0707631036639214, 0.0718683376908302, 0.071540854871273,
0.0993443354964256, 0.0973133370280266, 0.0668289735913277,
0.0731536969542503, 0.0717423185706139, 0.0750747397542,
0.0754749700427055, 0.0746353641152382, 0.0912141725420952,
0.100288398563862, 0.07671108096838, 0.0708827450871468,
0.0708827450871468, 0.0741974636912346, 0.0761196836829185,
0.0648202076554298, 0.114817388355732, 0.100519739091396,
0.100288398563862, 0.102664910256863, 0.0566539540886879,
0.0648743882775307, 0.0706770494580269, 0.0746353641152382,
0.0750747397542, 0.0750747397542, 0.113439425826073, 0.127338454127312,
0.114467553794384, 0.112322382628918, 0.136203452944756,
0.131634846329689, 0.144591823220253, 0.139709115028381,
0.140504062175751, 0.145617410540581, 0.166768744587898,
0.151759415864944, 0.151587069034576, 0.156320676207542,
0.131974145770073, 0.135847419500351, 0.159762278199196,
0.163790658116341, 0.158604919910431, 0.127949997782707,
0.109744042158127, 0.101787634193897, 0.0582009926438332,
0.0671374276280403, 0.0669510439038277, 0.074992410838604,
0.074992410838604, 0.0787725821137428, 0.0696783438324928,
0.0965322777628899, 0.0666131302714348, 0.0666131302714348,
0.0925824269652367, 0.0875711515545845, 0.0873847678303719,
0.0873847678303719, 0.0711322501301765, 0.0696783438324928,
0.0667495802044868, 0.0667495802044868, 0.0685289725661278,
0.0582009926438332, 0.0599979534745216, 0.0702869072556496,
0.068938173353672, 0.0610831864178181, 0.074992410838604,
0.0702869072556496, 0.0683296099305153, 0.0671374276280403,
0.0666131302714348, 0.0815844461321831, 0.070037417113781,
0.068938173353672, 0.0862899348139763, 0.0824234709143639,
0.0761196836829185, 0.0663527771830559, 0.0751885995268822,
0.072908379137516, 0.0599117167294025, 0.0743952021002769,
0.0666681602597237, 0.0667495802044868, 0.0748060271143913,
0.0800538137555122, 0.0787725821137428, 0.0741974636912346,
0.0741937384009361), date = structure(c(14610, 14611, 14612,
14613, 14614, 14615, 14616, 14617, 14618, 14619, 14620, 14621,
14622, 14623, 14624, 14625, 14626, 14627, 14628, 14629, 14630,
14631, 14632, 14633, 14634, 14635, 14636, 14637, 14638, 14639,
14640, 14641, 14642, 14643, 14644, 14645, 14646, 14647, 14648,
14649, 14650, 14651, 14652, 14653, 14654, 14655, 14656, 14657,
14658, 14659, 14660, 14661, 14662, 14663, 14664, 14665, 14666,
14667, 14668, 14669, 14670, 14671, 14672, 14673, 14674, 14675,
14676, 14677, 14678, 14679, 14680, 14681, 14682, 14683, 14684,
14685, 14686, 14687, 14688, 14689, 14690, 14691, 14692, 14693,
14694, 14695, 14696, 14697, 14698, 14699, 14700, 14701, 14702,
14703, 14704, 14705, 14706, 14707, 14708, 14709), class = "Date")), row.names = c(NA,
100L), class = "data.frame")
Adding a plane at the value of 0 for z:
library(plotly)
library(dplyr)
# I separated your df wrangling to better understand the data
df <- df %>%
select(date, everything())
# Code you posted
df %>%
plot_ly(
x = colnames(subset(., select = c(2:4))), # probably not the most efficient method
y = ~date,
z = data.matrix(subset(., select = c(2:4))),
type = "surface",
colors = c("darkblue", "yellow", "darkred")
) %>%
# The surface added by using y & x from df
# and making z a matrix of zeros 3 rows by 100 columns.
add_surface(z = matrix(0, ncol = 3, nrow = 100),
y = df$date,
x = colnames(subset(df, select = c(2:4))),
opacity = .8)
Your plot:
With added zero z plane:

maintain date class for x-axis with geom_smooth

I am trying to smooth three lines and shade the area between the min and max lines using this approach. My x-axis is a date class at the start, but I'm getting an error in the last step.
Invalid input: date_trans works with objects of class Date only
I'm losing the date class when smoothing into p1. How can I prevent this or otherwise pass a date to geom_ribbon in the final step?
library(tidyverse)
mmr <- structure(list(year = structure(c(3834, 3926, 3987, 4108, 4169,
4230, 4352, 4442, 4503, 4595, 4687, 4748, 4929, 4991, 5052, 5204,
5265, 5326, 5448, 5538, 5599, 5752, 5813, 5844, 6025, 6087, 6148,
6299, 6360, 6574, 6634, 6695, 6848, 6909, 7091, 7183, 7213, 7395,
7456, 7517, 7639, 7729, 7790, 7943, 8035, 8187, 8248, 8309, 8460,
8552, 8613, 8735, 8825, 8886, 9009, 9100, 9131, 9282, 9343, 9404,
9556, 9617, 9678, 9831, 9862, 9952, 10105, 10135, 10196, 10347,
10408, 10470, 10592, 10682, 10896, 10957, 11078, 11170, 11231,
11382, 11474, 11596, 11657, 11688, 11869, 11931, 11961, 12112,
12173, 12234, 12357, 12418, 12478, 12600, 12692, 12874, 12935,
12996, 13118, 13208, 13239, 13392, 13483, 13665, 13757, 13787,
13939), class = "Date"), mmrU = c(13.8231596, 13.7529708, 13.7148858,
13.6118164, 13.5638772, 13.5478975, 13.519933, 13.5012899, 13.474657,
13.4014166, 13.4173963, 13.3787786, 13.2882268, 13.2589306, 13.2354049,
13.1763686, 13.1470724, 13.1255442, 13.0618471, 13.032551, 13.0108008,
13.0139079, 13.0232294, 13.0352143, 13.0498623, 13.0498623, 13.0376556,
13.0232294, 13.0205662, 12.987275, 12.9739586, 12.9033814, 12.9792852,
12.99127, 13.0059181, 13.0059181, 13.0059181, 13.0205662, 13.0130202,
13.0034767, 12.9606421, 12.9406675, 12.9277949, 12.8727536, 12.8203756,
12.7728802, 12.7582321, 12.7569005, 12.7422524, 12.7382575, 12.7171414,
12.6397157, 12.601098, 12.5737993, 12.6583588, 12.7382575, 12.7979151,
12.9473257, 12.9965966, 13.0327729, 13.1191079, 13.1617205, 13.2061087,
13.332171, 13.39609, 13.4586773, 13.6477709, 13.7676189, 13.8408594,
14.0619124, 14.1604541, 14.2246394, 14.3815071, 14.4851535, 14.7996436,
14.9234866, 15.0436305, 15.3022444, 15.4277361, 15.7198102, 15.9745982,
16.2684478, 16.4362351, 16.5409911, 16.8277386, 16.9728879, 17.0634398,
17.3204472, 17.4616016, 17.5578129, 17.819814, 17.9623, 18.0631721,
18.3045327, 18.4784455, 18.6694034, 18.7612869, 18.8153517, 18.9250792,
18.9770134, 19.0189602, 19.0675652, 19.07955, 19.1008563, 19.0582437,
19.0292138, 18.9410589), mmr = c(12.1431454, 12.11401197, 12.05086241,
12.02612341, 11.96590348, 11.93823486, 11.8828976, 11.8601117,
11.8275604, 11.8047745, 11.7784079, 11.7461821, 11.6955796, 11.7256748,
11.6689467, 11.6396505, 11.5997012, 11.6033632, 11.5384455, 11.5144759,
11.4532202, 11.4532202, 11.5411088, 11.5198025, 11.4971645, 11.5118126,
11.5118126, 11.5345985, 11.5586865, 11.4532202, 11.4532202, 11.4692,
11.5144759, 11.5201829, 11.5494791, 11.525129, 11.5662198, 11.570405,
11.4665367, 11.4984962, 11.4345772, 11.4692, 11.4092759, 11.3893013,
11.3653317, 11.3799798, 11.4406647, 11.3014127, 11.2721165, 11.2833024,
11.2720179, 11.2161875, 11.1675824, 11.1749065, 11.1928097, 11.2546572,
11.3295252, 11.397883, 11.568574, 11.5020472, 11.5437721, 11.5792826,
11.6846601, 11.7494372, 11.8112847, 11.8868038, 12.0684401, 12.1749717,
12.2844995, 12.3747184, 12.4756571, 12.5664753, 12.7422524, 12.8274777,
13.114314, 13.2216445, 13.3164576, 13.575862, 13.7156847, 13.8555074,
14.1513989, 14.2922869, 14.4147983, 14.5559526, 14.8196183, 14.9421296,
15.0779574, 15.203132, 15.3283066, 15.4521496, 15.686519, 15.7970456,
15.9248835, 16.0292844, 16.21678, 16.3945102, 16.4693782, 16.5442463,
16.6028386, 16.661431, 16.7265336, 16.7734075, 16.8304019, 16.8407888,
16.781464, 16.7411817, 16.6532932), mmrL = c(10.68619282, 10.63692196,
10.5963068, 10.49976255, 10.45848156, 10.44250182, 10.4092107,
10.39189932, 10.38390945, 10.31732721, 10.28270445, 10.25695931,
10.20813234, 10.21612221, 10.22278043, 10.25207662, 10.26139813,
10.24719392, 10.21079563, 10.19481589, 10.17883615, 10.17883615,
10.17883615, 10.17883615, 10.1921526, 10.19481589, 10.20813234,
10.20946398, 10.22011714, 10.19481589, 10.18283109, 10.17883615,
10.17883615, 10.17883615, 10.17883615, 10.17484122, 10.16418806,
10.14554503, 10.13356023, 10.12024378, 10.09893747, 10.08828431,
10.0762995, 10.06165141, 10.04700332, 10.02436536, 9.999064105,
9.9810869, 9.936476799, 9.911175548, 9.890756995, 9.840598374,
9.815297123, 9.79798574, 9.880547718, 9.947129958, 9.997199802,
10.1149172, 10.14021845, 10.14953997, 10.18948931, 10.21345892,
10.24963527, 10.36659807, 10.42519044, 10.47179801, 10.63958525,
10.74478519, 10.82335224, 10.99261909, 11.11726527, 11.15293432,
11.27611147, 11.33869877, 11.52113411, 11.58638471, 11.6377862,
11.82075419, 11.96190854, 12.31346277, 12.45195383, 12.52985505,
12.74491568, 12.85810549, 12.93633962, 13.16971038, 13.29887992,
13.38676848, 13.61581138, 13.71834803, 13.78226698, 13.95005423,
14.03927443, 14.10159541, 14.27497556, 14.42971269, 14.57725893,
14.60788676, 14.63185637, 14.69843861, 14.72373986, 14.73439302,
14.73439302, 14.73439302, 14.73683436, 14.674469, 14.60744288
)), row.names = c(NA, -107L), class = "data.frame")
# smooth lines
p1 <-
mmr %>%
ggplot(.) +
geom_smooth(aes(x=year, y=mmrL)) +
geom_smooth(aes(x=year, y=mmr)) +
geom_smooth(aes(x=year, y=mmrU)) +
ylim(0, 25) +
scale_x_date(date_breaks="2 years", date_labels = "%Y")
# build plot object for rendering
pp1 <- ggplot_build(p1)
# extract data from the upper and lower lines
df2 <- data.frame(x = pp1$data[[1]]$x,
ymin = pp1$data[[2]]$y,
ymax = pp1$data[[3]]$y)
# use the lm data to add the ribbon to the plot
p1 + geom_ribbon(data = df2, aes(x = x, ymin = ymin, ymax = ymax), fill = "grey", alpha = 0.4)
The issue is that pp1$data[[1]]$x is no longer of date format. We may instead use
df2 <- data.frame(x = as.Date(pp1$data[[1]]$x, origin = "1970-01-01"),
ymin = pp1$data[[1]]$y,
ymax = pp1$data[[3]]$y)
where I've also adjusted ymin. This gives

Data sorting with R for XLConnect

Ok, so I have a workbook in Excel with sheets that look like this:
Date 1/2/15 1/3/15 1/4/15
Euro 3.54 2.50 #N/A
USD 3.20 3.30 3.35
Yen 2.50 2.35 2.40
The sheets are arranged exactly like this. I loaded each sheet individually via XLConnect such that:
wbFX <- loadWorkbook("fx.xlsx")
FX.high <- readWorksheet(wbFX, sheet=1)
FX.low <- readWorksheet(wbFX, sheet=2)
FX.close <- readWorksheet(wbFX, sheet=3)
One sheet is for closing prices, the other for low and the last one is closing prices.
I want to merge the currency rows (i.e: grouping Euro low, Euro high, Euro close) and create a table or dataframe such that I have:
Date 1/2/15 1/3/15 1/4/15
Euro close 3.54 2.50 #N/A
Euro low 3.20 3.30 3.35
Euro high 2.50 2.35 2.40
I only have very rudimentary knowledge of R and I'm not very familiar with the for() loop function in R. I understand the basics of it, but I'm struggling to reproduce what I want.
Suggestions would be very much appreciated!
UPDATED: Adding dput(FX.high) gives:
> dput(head(FX.high))
structure(list(Col1 = c("EUR CURNCY", "JPY CURNCY", "GBP CURNCY",
"CHF CURNCY", "AUD CURNCY", "CAD CURNCY"), X2016.08.30.00.00.00 = c(1.1192,
102.56, 1.312, 0.9815, 0.758, 1.3056), X2016.08.29.00.00.00 = c(1.1208,
102.39, 1.3172, 0.9807, 0.7582, 1.3048), X2016.08.28.00.00.00 = c(1.1341,
101.94, 1.3279, 0.9793, 0.7692, 1.3012), X2016.08.27.00.00.00 = c(1.1341,
101.94, 1.3279, 0.9793, 0.7692, 1.3012), X2016.08.26.00.00.00 = c(1.1341,
101.94, 1.3279, 0.9793, 0.7692, 1.3012), X2016.08.25.00.00.00 = c(1.1298,
100.62, 1.3264, 0.9688, 0.7639, 1.294), X2016.08.24.00.00.00 = c(1.1312,
100.61, 1.3273, 0.9684, 0.7634, 1.2958), X2016.08.23.00.00.00 = c(1.1355,
100.39, 1.3211, 0.9634, 0.7655, 1.2948), X2016.08.22.00.00.00 = c(1.1334,
100.93, 1.3157, 0.9649, 0.764, 1.2965), X2016.08.21.00.00.00 = c(1.136,
100.46, 1.3185, 0.9611, 0.7691, 1.2892), X2016.08.20.00.00.00 = c(1.136,
100.46, 1.3185, 0.9611, 0.7691, 1.2892), X2016.08.19.00.00.00 = c(1.136,
100.46, 1.3185, 0.9611, 0.7691, 1.2892), X2016.08.18.00.00.00 = c(1.1366,
100.5, 1.3173, 0.9627, 0.7723, 1.2858), X2016.08.17.00.00.00 = c(1.1316,
101.17, 1.3086, 0.9659, 0.7708, 1.2918), X2016.08.16.00.00.00 = c(1.1323,
101.29, 1.3051, 0.9735, 0.7749, 1.2934), X2016.08.15.00.00.00 = c(1.1204,
101.45, 1.2945, 0.9775, 0.7692, 1.2976), X2016.08.14.00.00.00 = c(1.1221,
102.27, 1.3035, 0.9766, 0.7725, 1.2994), X2016.08.13.00.00.00 = c(1.1221,
102.27, 1.3035, 0.9766, 0.7725, 1.2994), X2016.08.12.00.00.00 = c(1.1221,
102.27, 1.3035, 0.9766, 0.7725, 1.2994), X2016.08.11.00.00.00 = c(1.1192,
102.06, 1.3028, 0.9766, 0.775, 1.308), X2016.08.10.00.00.00 = c(1.119,
101.96, 1.3094, 0.9819, 0.7756, 1.3124), X2016.08.09.00.00.00 = c(1.1123,
102.53, 1.3049, 0.9844, 0.7687, 1.319), X2016.08.08.00.00.00 = c(1.1105,
102.66, 1.3097, 0.9842, 0.7672, 1.319), X2016.08.07.00.00.00 = c(1.1161,
102.06, 1.3175, 0.9831, 0.7664, 1.32), X2016.08.06.00.00.00 = c(1.1161,
102.06, 1.3175, 0.9831, 0.7664, 1.32), X2016.08.05.00.00.00 = c(1.1161,
102.06, 1.3175, 0.9831, 0.7664, 1.32), X2016.08.04.00.00.00 = c(1.1156,
101.67, 1.3346, 0.975, 0.7641, 1.3089), X2016.08.03.00.00.00 = c(1.1227,
101.57, 1.3372, 0.9739, 0.7616, 1.3148), X2016.08.02.00.00.00 = c(1.1234,
102.83, 1.3366, 0.9698, 0.7638, 1.3142), X2016.08.01.00.00.00 = c(1.1184,
102.68, 1.3273, 0.9703, 0.7615, 1.3127), X2016.07.31.00.00.00 = c(1.1197,
105.63, 1.3301, 0.981, 0.761, 1.3186), X2016.07.30.00.00.00 = c(1.1197,
105.63, 1.3301, 0.981, 0.761, 1.3186), X2016.07.29.00.00.00 = c(1.1197,
105.63, 1.3301, 0.981, 0.761, 1.3186), X2016.07.28.00.00.00 = c(1.1119,
105.51, 1.3248, 0.9868, 0.7549, 1.3192), X2016.07.27.00.00.00 = c(1.1065,
106.54, 1.3235, 0.995, 0.7566, 1.3253), X2016.07.26.00.00.00 = c(1.103,
105.89, 1.3176, 0.9928, 0.754, 1.3244), X2016.07.25.00.00.00 = c(1.0999,
106.72, 1.3165, 0.9897, 0.7492, 1.3242), X2016.07.24.00.00.00 = c(1.1041,
106.4, 1.3291, 0.9895, 0.7508, 1.3185), X2016.07.23.00.00.00 = c(1.1041,
106.4, 1.3291, 0.9895, 0.7508, 1.3185), X2016.07.22.00.00.00 = c(1.1041,
106.4, 1.3291, 0.9895, 0.7508, 1.3185), X2016.07.21.00.00.00 = c(1.106,
107.49, 1.3275, 0.9907, 0.7514, 1.3101), X2016.07.20.00.00.00 = c(1.103,
107.02, 1.3226, 0.9905, 0.7517, 1.3096), X2016.07.19.00.00.00 = c(1.1081,
106.53, 1.3276, 0.9878, 0.7592, 1.3054), X2016.07.18.00.00.00 = c(1.1084,
106.26, 1.3315, 0.9847, 0.7607, 1.3022), X2016.07.17.00.00.00 = c(1.1149,
106.32, 1.3481, 0.9847, 0.7676, 1.2988), X2016.07.16.00.00.00 = c(1.1149,
106.32, 1.3481, 0.9847, 0.7676, 1.2988), X2016.07.15.00.00.00 = c(1.1149,
106.32, 1.3481, 0.9847, 0.7676, 1.2988), X2016.07.14.00.00.00 = c(1.1165,
105.94, 1.3475, 0.9854, 0.7653, 1.2987), X2016.07.13.00.00.00 = c(1.112,
104.88, 1.3338, 0.9894, 0.7638, 1.3084), X2016.07.12.00.00.00 = c(1.1126,
104.99, 1.3295, 0.9894, 0.7658, 1.3133), X2016.07.11.00.00.00 = c(1.1075,
102.89, 1.3018, 0.9858, 0.7576, 1.314), X2016.07.10.00.00.00 = c(1.112,
101.28, 1.3019, 0.9866, 0.7574, 1.309), X2016.07.09.00.00.00 = c(1.112,
101.28, 1.3019, 0.9866, 0.7574, 1.309), X2016.07.08.00.00.00 = c(1.112,
101.28, 1.3019, 0.9866, 0.7574, 1.309), X2016.07.07.00.00.00 = c(1.1107,
101.41, 1.3047, 0.9792, 0.7539, 1.3021), X2016.07.06.00.00.00 = c(1.1112,
101.77, 1.3028, 0.9806, 0.7529, 1.3056), X2016.07.05.00.00.00 = c(1.1186,
102.59, 1.3291, 0.9774, 0.7545, 1.3017), X2016.07.04.00.00.00 = c(1.116,
102.8, 1.3341, 0.9759, 0.7545, 1.2926), X2016.07.03.00.00.00 = c(1.1169,
103.39, 1.335, 0.9781, 0.7503, 1.2975), X2016.07.02.00.00.00 = c(1.1169,
103.39, 1.335, 0.9781, 0.7503, 1.2975), X2016.07.01.00.00.00 = c(1.1169,
103.39, 1.335, 0.9781, 0.7503, 1.2975), X2016.06.30.00.00.00 = c(1.1155,
103.29, 1.3496, 0.9821, 0.7473, 1.3016), X2016.06.29.00.00.00 = c(1.113,
102.94, 1.3534, 0.9823, 0.7456, 1.3042), X2016.06.28.00.00.00 = c(1.1112,
102.84, 1.3419, 0.9837, 0.7415, 1.3108), X2016.06.27.00.00.00 = c(1.1084,
102.48, 1.3566, 0.9819, 0.7459, 1.312), X2016.06.26.00.00.00 = c(1.1428,
106.84, 1.5018, 0.9804, 0.7648, 1.3099), X2016.06.25.00.00.00 = c(1.1428,
106.84, 1.5018, 0.9804, 0.7648, 1.3099), X2016.06.24.00.00.00 = c(1.1428,
106.84, 1.5018, 0.9804, 0.7648, 1.3099), X2016.06.23.00.00.00 = c(1.1421,
106.17, 1.4947, 0.9602, 0.7616, 1.2847), X2016.06.22.00.00.00 = c(1.1338,
104.85, 1.4774, 0.9629, 0.7527, 1.2853), X2016.06.21.00.00.00 = c(1.135,
105.06, 1.4783, 0.9624, 0.7513, 1.2827), X2016.06.20.00.00.00 = c(1.1382,
104.85, 1.472, 0.9643, 0.7481, 1.2889), X2016.06.19.00.00.00 = c(1.1296,
104.83, 1.4388, 0.9659, 0.7411, 1.2968), X2016.06.18.00.00.00 = c(1.1296,
104.83, 1.4388, 0.9659, 0.7411, 1.2968), X2016.06.17.00.00.00 = c(1.1296,
104.83, 1.4388, 0.9659, 0.7411, 1.2968), X2016.06.16.00.00.00 = c(1.1295,
106.03, 1.4254, 0.9687, 0.744, 1.3086), X2016.06.15.00.00.00 = c(1.1298,
106.4, 1.4218, 0.9664, 0.7446, 1.2943), X2016.06.14.00.00.00 = c(1.1298,
106.42, 1.4271, 0.9669, 0.7405, 1.2873), X2016.06.13.00.00.00 = c(1.1303,
106.98, 1.4328, 0.9679, 0.7411, 1.2839), X2016.06.12.00.00.00 = c(1.1321,
107.26, 1.4473, 0.9658, 0.7438, 1.2784), X2016.06.11.00.00.00 = c(1.1321,
107.26, 1.4473, 0.9658, 0.7438, 1.2784), X2016.06.10.00.00.00 = c(1.1321,
107.26, 1.4473, 0.9658, 0.7438, 1.2784), X2016.06.09.00.00.00 = c(1.1416,
107.18, 1.4527, 0.9656, 0.7505, 1.2767), X2016.06.08.00.00.00 = c(1.1411,
107.39, 1.4601, 0.9659, 0.7482, 1.2761), X2016.06.07.00.00.00 = c(1.1381,
107.9, 1.466, 0.9719, 0.7464, 1.284), X2016.06.06.00.00.00 = c(1.1393,
107.66, 1.4529, 0.978, 0.7391, 1.2983), X2016.06.05.00.00.00 = c(1.1374,
109.14, 1.4582, 0.992, 0.7369, 1.3107), X2016.06.04.00.00.00 = c(1.1374,
109.14, 1.4582, 0.992, 0.7369, 1.3107), X2016.06.03.00.00.00 = c(1.1374,
109.14, 1.4582, 0.992, 0.7369, 1.3107), X2016.06.02.00.00.00 = c(1.122,
109.59, 1.4473, 0.9911, 0.727, 1.3144), X2016.06.01.00.00.00 = c(1.1194,
110.83, 1.4508, 0.9951, 0.7299, 1.3123), X2016.05.31.00.00.00 = c(1.1173,
111.35, 1.4725, 0.9951, 0.7267, 1.3134), X2016.05.30.00.00.00 = c(1.1145,
111.45, 1.4642, 0.9956, 0.7188, 1.3095), X2016.05.29.00.00.00 = c(1.1201,
110.45, 1.4689, 0.9949, 0.7235, 1.3068), X2016.05.28.00.00.00 = c(1.1201,
110.45, 1.4689, 0.9949, 0.7235, 1.3068), X2016.05.27.00.00.00 = c(1.1201,
110.45, 1.4689, 0.9949, 0.7235, 1.3068), X2016.05.26.00.00.00 = c(1.1217,
110.23, 1.474, 0.9928, 0.7244, 1.3037), X2016.05.25.00.00.00 = c(1.1167,
110.45, 1.4729, 0.9936, 0.7219, 1.3133), X2016.05.24.00.00.00 = c(1.1227,
110.13, 1.4642, 0.9938, 0.7228, 1.3188), X2016.05.23.00.00.00 = c(1.1243,
110.24, 1.4549, 0.9924, 0.726, 1.3174), X2016.05.22.00.00.00 = c(1.1237,
110.59, 1.4613, 0.9927, 0.725, 1.3162), X2016.05.21.00.00.00 = c(1.1237,
110.59, 1.4613, 0.9927, 0.725, 1.3162), X2016.05.20.00.00.00 = c(1.1237,
110.59, 1.4613, 0.9927, 0.725, 1.3162), X2016.05.19.00.00.00 = c(1.123,
110.38, 1.4663, 0.9923, 0.7242, 1.3154), X2016.05.18.00.00.00 = c(1.1316,
110.26, 1.4635, 0.9881, 0.7332, 1.3037), X2016.05.17.00.00.00 = c(1.1349,
109.65, 1.4524, 0.9809, 0.7366, 1.2955), X2016.05.16.00.00.00 = c(1.1342,
109.1, 1.4415, 0.9784, 0.7309, 1.2963), X2016.05.15.00.00.00 = c(1.138,
109.56, 1.4456, 0.9775, 0.7326, 1.2958), X2016.05.14.00.00.00 = c(1.138,
109.56, 1.4456, 0.9775, 0.7326, 1.2958), X2016.05.13.00.00.00 = c(1.138,
109.56, 1.4456, 0.9775, 0.7326, 1.2958), X2016.05.12.00.00.00 = c(1.1429,
109.4, 1.4531, 0.9726, 0.738, 1.2879), X2016.05.11.00.00.00 = c(1.1447,
109.38, 1.4488, 0.9762, 0.7402, 1.2942), X2016.05.10.00.00.00 = c(1.141,
109.35, 1.4478, 0.9763, 0.7374, 1.298), X2016.05.09.00.00.00 = c(1.142,
108.6, 1.448, 0.9736, 0.7385, 1.3015), X2016.05.08.00.00.00 = c(1.1479,
107.42, 1.4543, 0.973, 0.7478, 1.2952), X2016.05.07.00.00.00 = c(1.1479,
107.42, 1.4543, 0.973, 0.7478, 1.2952), X2016.05.06.00.00.00 = c(1.1479,
107.42, 1.4543, 0.973, 0.7478, 1.2952), X2016.05.05.00.00.00 = c(1.1494,
107.5, 1.4529, 0.9697, 0.7514, 1.2875), X2016.05.04.00.00.00 = c(1.1529,
107.46, 1.4572, 0.959, 0.7517, 1.2886), X2016.05.03.00.00.00 = c(1.1616,
106.68, 1.477, 0.9554, 0.7719, 1.2732), X2016.05.02.00.00.00 = c(1.1536,
106.82, 1.4696, 0.9605, 0.7672, 1.2571), X2016.05.01.00.00.00 = c(1.1459,
108.2, 1.467, 0.9672, 0.7669, 1.2587), X2016.04.30.00.00.00 = c(1.1459,
108.2, 1.467, 0.9672, 0.7669, 1.2587), X2016.04.29.00.00.00 = c(1.1459,
108.2, 1.467, 0.9672, 0.7669, 1.2587), X2016.04.28.00.00.00 = c(1.1368,
111.88, 1.4623, 0.9734, 0.7658, 1.2606), X2016.04.27.00.00.00 = c(1.1362,
111.75, 1.4622, 0.9753, 0.7765, 1.2694), X2016.04.26.00.00.00 = c(1.134,
111.47, 1.4639, 0.9767, 0.7765, 1.2688), X2016.04.25.00.00.00 = c(1.1278,
111.91, 1.452, 0.9794, 0.7729, 1.2717), X2016.04.24.00.00.00 = c(1.1309,
111.81, 1.4452, 0.9797, 0.7774, 1.2758), X2016.04.23.00.00.00 = c(1.1309,
111.81, 1.4452, 0.9797, 0.7774, 1.2758), X2016.04.22.00.00.00 = c(1.1309,
111.81, 1.4452, 0.9797, 0.7774, 1.2758), X2016.04.21.00.00.00 = c(1.1398,
109.9, 1.444, 0.9753, 0.7835, 1.275), X2016.04.20.00.00.00 = c(1.1388,
109.88, 1.441, 0.9733, 0.7829, 1.273), X2016.04.19.00.00.00 = c(1.1385,
109.49, 1.4419, 0.965, 0.7826, 1.2798), X2016.04.18.00.00.00 = c(1.1332,
108.99, 1.4291, 0.9679, 0.7759, 1.299), X2016.04.17.00.00.00 = c(1.1317,
109.73, 1.4242, 0.9688, 0.7734, 1.2903), X2016.04.16.00.00.00 = c(1.1317,
109.73, 1.4242, 0.9688, 0.7734, 1.2903), X2016.04.15.00.00.00 = c(1.1317,
109.73, 1.4242, 0.9688, 0.7734, 1.2903), X2016.04.14.00.00.00 = c(1.1295,
109.55, 1.4208, 0.9688, 0.7737, 1.2897), X2016.04.13.00.00.00 = c(1.1391,
109.41, 1.4279, 0.9672, 0.7716, 1.2828), X2016.04.12.00.00.00 = c(1.1465,
108.79, 1.4348, 0.9594, 0.769, 1.2921), X2016.04.11.00.00.00 = c(1.1447,
108.44, 1.4287, 0.9571, 0.763, 1.3016), X2016.04.10.00.00.00 = c(1.1419,
109.1, 1.4141, 0.9582, 0.7579, 1.3157), X2016.04.09.00.00.00 = c(1.1419,
109.1, 1.4141, 0.9582, 0.7579, 1.3157), X2016.04.08.00.00.00 = c(1.1419,
109.1, 1.4141, 0.9582, 0.7579, 1.3157), X2016.04.07.00.00.00 = c(1.1454,
109.9, 1.4157, 0.9581, 0.7637, 1.3181), X2016.04.06.00.00.00 = c(1.1432,
110.64, 1.4171, 0.9622, 0.7619, 1.3187), X2016.04.05.00.00.00 = c(1.1405,
111.36, 1.4279, 0.9605, 0.7632, 1.3219), X2016.04.04.00.00.00 = c(1.1413,
111.8, 1.4322, 0.9615, 0.7679, 1.3088), X2016.04.03.00.00.00 = c(1.1438,
112.58, 1.4372, 0.9626, 0.7701, 1.3147), X2016.04.02.00.00.00 = c(1.1438,
112.58, 1.4372, 0.9626, 0.7701, 1.3147), X2016.04.01.00.00.00 = c(1.1438,
112.58, 1.4372, 0.9626, 0.7701, 1.3147), X2016.03.31.00.00.00 = c(1.1412,
112.66, 1.4426, 0.9663, 0.7723, 1.3011), X2016.03.30.00.00.00 = c(1.1365,
112.81, 1.4459, 0.9672, 0.7709, 1.3081), X2016.03.29.00.00.00 = c(1.1303,
113.8, 1.4404, 0.9763, 0.7645, 1.3216), X2016.03.28.00.00.00 = c(1.122,
113.69, 1.4283, 0.9787, 0.7558, 1.3285), X2016.03.27.00.00.00 = c(1.1181,
113.32, 1.4159, 0.9788, 0.7535, 1.3285), X2016.03.26.00.00.00 = c(1.1181,
113.32, 1.4159, 0.9788, 0.7535, 1.3285), X2016.03.25.00.00.00 = c(1.1181,
113.32, 1.4159, 0.9788, 0.7535, 1.3285), X2016.03.24.00.00.00 = c(1.1188,
113.01, 1.4183, 0.9774, 0.7538, 1.3296), X2016.03.23.00.00.00 = c(1.1224,
112.91, 1.4227, 0.9766, 0.7649, 1.3219), X2016.03.22.00.00.00 = c(1.126,
112.49, 1.4398, 0.9736, 0.7643, 1.3139), X2016.03.21.00.00.00 = c(1.1288,
111.98, 1.4469, 0.9731, 0.7627, 1.3102), X2016.03.20.00.00.00 = c(1.1337,
111.76, 1.4514, 0.9717, 0.768, 1.3043), X2016.03.19.00.00.00 = c(1.1337,
111.76, 1.4514, 0.9717, 0.768, 1.3043), X2016.03.18.00.00.00 = c(1.1337,
111.76, 1.4514, 0.9717, 0.768, 1.3043), X2016.03.17.00.00.00 = c(1.1342,
112.96, 1.4503, 0.9787, 0.7657, 1.3133), X2016.03.16.00.00.00 = c(1.1242,
113.82, 1.4274, 0.9914, 0.7561, 1.3405), X2016.03.15.00.00.00 = c(1.1125,
114.14, 1.4306, 0.9896, 0.7528, 1.3402), X2016.03.14.00.00.00 = c(1.1176,
114.01, 1.4401, 0.9882, 0.7594, 1.3308), X2016.03.13.00.00.00 = c(1.121,
113.92, 1.4437, 0.9891, 0.7584, 1.3359), X2016.03.12.00.00.00 = c(1.121,
113.92, 1.4437, 0.9891, 0.7584, 1.3359), X2016.03.11.00.00.00 = c(1.121,
113.92, 1.4437, 0.9891, 0.7584, 1.3359), X2016.03.10.00.00.00 = c(1.1218,
114.45, 1.4317, 1.0093, 0.7512, 1.3398), X2016.03.09.00.00.00 = c(1.1035,
113.45, 1.4241, 1.0039, 0.7528, 1.3446), X2016.03.08.00.00.00 = c(1.1058,
113.52, 1.4276, 0.9971, 0.7473, 1.3425), X2016.03.07.00.00.00 = c(1.1026,
114.09, 1.4284, 1.0012, 0.7485, 1.3377), X2016.03.06.00.00.00 = c(1.1043,
114.26, 1.4248, 0.9989, 0.7443, 1.3471), X2016.03.05.00.00.00 = c(1.1043,
114.26, 1.4248, 0.9989, 0.7443, 1.3471), X2016.03.04.00.00.00 = c(1.1043,
114.26, 1.4248, 0.9989, 0.7443, 1.3471), X2016.03.03.00.00.00 = c(1.0973,
114.27, 1.4194, 0.9983, 0.7374, 1.3473), X2016.03.02.00.00.00 = c(1.0881,
114.56, 1.4093, 1.0009, 0.7301, 1.3499), X2016.03.01.00.00.00 = c(1.0894,
114.19, 1.4018, 1.0009, 0.7192, 1.3552), X2016.02.29.00.00.00 = c(1.0963,
113.99, 1.3946, 1.0038, 0.7168, 1.3587), X2016.02.28.00.00.00 = c(1.1068,
114, 1.4043, 0.9989, 0.7257, 1.3565), X2016.02.27.00.00.00 = c(1.1068,
114, 1.4043, 0.9989, 0.7257, 1.3565), X2016.02.26.00.00.00 = c(1.1068,
114, 1.4043, 0.9989, 0.7257, 1.3565), X2016.02.25.00.00.00 = c(1.105,
113.02, 1.3997, 0.9952, 0.7244, 1.3735), X2016.02.24.00.00.00 = c(1.1046,
112.27, 1.4028, 0.9953, 0.7213, 1.3859), X2016.02.23.00.00.00 = c(1.1053,
113.05, 1.4156, 1.0002, 0.7259, 1.3821), X2016.02.22.00.00.00 = c(1.1135,
113.39, 1.4332, 1.0004, 0.7247, 1.3813), X2016.02.21.00.00.00 = c(1.1139,
113.38, 1.4409, 0.9968, 0.7162, 1.3847), X2016.02.20.00.00.00 = c(1.1139,
113.38, 1.4409, 0.9968, 0.7162, 1.3847), X2016.02.19.00.00.00 = c(1.1139,
113.38, 1.4409, 0.9968, 0.7162, 1.3847), X2016.02.18.00.00.00 = c(1.115,
114.33, 1.4394, 0.9969, 0.7185, 1.3752), X2016.02.17.00.00.00 = c(1.1179,
114.51, 1.4339, 0.9942, 0.7187, 1.3899), X2016.02.16.00.00.00 = c(1.1193,
114.87, 1.4516, 0.9896, 0.7182, 1.3912), X2016.02.15.00.00.00 = c(1.1261,
114.73, 1.4567, 0.9889, 0.7172, 1.3867), X2016.02.14.00.00.00 = c(1.1334,
113.54, 1.457, 0.9791, 0.7129, 1.3965), X2016.02.13.00.00.00 = c(1.1334,
113.54, 1.457, 0.9791, 0.7129, 1.3965), X2016.02.12.00.00.00 = c(1.1334,
113.54, 1.457, 0.9791, 0.7129, 1.3965), X2016.02.11.00.00.00 = c(1.1376,
113.6, 1.4564, 0.9762, 0.7153, 1.4016), X2016.02.10.00.00.00 = c(1.1311,
115.26, 1.4578, 0.982, 0.7125, 1.3999), X2016.02.09.00.00.00 = c(1.1338,
115.85, 1.4516, 0.9875, 0.7096, 1.396), X2016.02.08.00.00.00 = c(1.1216,
117.53, 1.4547, 0.9973, 0.7129, 1.3978), X2016.02.07.00.00.00 = c(1.1246,
117.43, 1.4592, 0.9985, 0.7219, 1.3919), X2016.02.06.00.00.00 = c(1.1246,
117.43, 1.4592, 0.9985, 0.7219, 1.3919), X2016.02.05.00.00.00 = c(1.1246,
117.43, 1.4592, 0.9985, 0.7219, 1.3919), X2016.02.04.00.00.00 = c(1.1239,
118.24, 1.4668, 1.0074, 0.7243, 1.3798), X2016.02.03.00.00.00 = c(1.1146,
120.04, 1.4649, 1.0196, 0.7189, 1.4103), X2016.02.02.00.00.00 = c(1.094,
121.04, 1.4446, 1.0224, 0.7129, 1.4082), X2016.02.01.00.00.00 = c(1.0913,
121.49, 1.4445, 1.025, 0.7121, 1.4062), X2016.01.31.00.00.00 = c(1.0949,
121.69, 1.4413, 1.0257, 0.7141, 1.4109), X2016.01.30.00.00.00 = c(1.0949,
121.69, 1.4413, 1.0257, 0.7141, 1.4109), X2016.01.29.00.00.00 = c(1.0949,
121.69, 1.4413, 1.0257, 0.7141, 1.4109), X2016.01.28.00.00.00 = c(1.0968,
118.99, 1.4408, 1.0178, 0.7129, 1.4123), X2016.01.27.00.00.00 = c(1.0917,
119.07, 1.4355, 1.0189, 0.7082, 1.4157), X2016.01.26.00.00.00 = c(1.0874,
118.62, 1.4367, 1.0199, 0.7021, 1.4326), X2016.01.25.00.00.00 = c(1.0857,
118.86, 1.4332, 1.0184, 0.7032, 1.4293), X2016.01.24.00.00.00 = c(1.0877,
118.88, 1.4363, 1.0166, 0.7046, 1.4301), X2016.01.23.00.00.00 = c(1.0877,
118.88, 1.4363, 1.0166, 0.7046, 1.4301), X2016.01.22.00.00.00 = c(1.0877,
118.88, 1.4363, 1.0166, 0.7046, 1.4301), X2016.01.21.00.00.00 = c(1.0921,
117.81, 1.4249, 1.0147, 0.7018, 1.4541), X2016.01.20.00.00.00 = c(1.0976,
117.69, 1.4219, 1.0058, 0.6926, 1.469), X2016.01.19.00.00.00 = c(1.0939,
118.11, 1.434, 1.0082, 0.6957, 1.4589), X2016.01.18.00.00.00 = c(1.0942,
117.44, 1.4323, 1.0073, 0.6928, 1.466), X2016.01.17.00.00.00 = c(1.0985,
118.27, 1.4428, 1.0061, 0.7002, 1.4554), X2016.01.16.00.00.00 = c(1.0985,
118.27, 1.4428, 1.0061, 0.7002, 1.4554), X2016.01.15.00.00.00 = c(1.0985,
118.27, 1.4428, 1.0061, 0.7002, 1.4554), X2016.01.14.00.00.00 = c(1.0943,
118.28, 1.4445, 1.0092, 0.6997, 1.4397), X2016.01.13.00.00.00 = c(1.0888,
118.38, 1.4476, 1.0107, 0.7049, 1.438), X2016.01.12.00.00.00 = c(1.09,
118.07, 1.456, 1.0047, 0.7021, 1.4315), X2016.01.11.00.00.00 = c(1.097,
118.02, 1.4604, 1.0023, 0.7036, 1.4246), X2016.01.10.00.00.00 = c(1.0934,
118.83, 1.4645, 1.0052, 0.7077, 1.4178), X2016.01.09.00.00.00 = c(1.0934,
118.83, 1.4645, 1.0052, 0.7077, 1.4178), X2016.01.08.00.00.00 = c(1.0934,
118.83, 1.4645, 1.0052, 0.7077, 1.4178), X2016.01.07.00.00.00 = c(1.094,
118.76, 1.4641, 1.0081, 0.7086, 1.417), X2016.01.06.00.00.00 = c(1.0799,
119.17, 1.4682, 1.0121, 0.7172, 1.4109), X2016.01.05.00.00.00 = c(1.0839,
119.7, 1.4726, 1.0125, 0.7215, 1.4019), X2016.01.04.00.00.00 = c(1.0946,
120.47, 1.4816, 1.0063, 0.7305, 1.3983), X2016.01.03.00.00.00 = c(1.0867,
120.55, 1.476, 1.0083, 0.7304, 1.3856), X2016.01.02.00.00.00 = c(1.0867,
120.55, 1.476, 1.0083, 0.7304, 1.3856), X2016.01.01.00.00.00 = c(1.0867,
120.55, 1.476, 1.0083, 0.7304, 1.3856)), .Names = c("Col1", "X2016.08.30.00.00.00",
"X2016.08.29.00.00.00", "X2016.08.28.00.00.00", "X2016.08.27.00.00.00",
"X2016.08.26.00.00.00", "X2016.08.25.00.00.00", "X2016.08.24.00.00.00",
"X2016.08.23.00.00.00", "X2016.08.22.00.00.00", "X2016.08.21.00.00.00",
"X2016.08.20.00.00.00", "X2016.08.19.00.00.00", "X2016.08.18.00.00.00",
"X2016.08.17.00.00.00", "X2016.08.16.00.00.00", "X2016.08.15.00.00.00",
"X2016.08.14.00.00.00", "X2016.08.13.00.00.00", "X2016.08.12.00.00.00",
"X2016.08.11.00.00.00", "X2016.08.10.00.00.00", "X2016.08.09.00.00.00",
"X2016.08.08.00.00.00", "X2016.08.07.00.00.00", "X2016.08.06.00.00.00",
"X2016.08.05.00.00.00", "X2016.08.04.00.00.00", "X2016.08.03.00.00.00",
"X2016.08.02.00.00.00", "X2016.08.01.00.00.00", "X2016.07.31.00.00.00",
"X2016.07.30.00.00.00", "X2016.07.29.00.00.00", "X2016.07.28.00.00.00",
"X2016.07.27.00.00.00", "X2016.07.26.00.00.00", "X2016.07.25.00.00.00",
"X2016.07.24.00.00.00", "X2016.07.23.00.00.00", "X2016.07.22.00.00.00",
"X2016.07.21.00.00.00", "X2016.07.20.00.00.00", "X2016.07.19.00.00.00",
"X2016.07.18.00.00.00", "X2016.07.17.00.00.00", "X2016.07.16.00.00.00",
"X2016.07.15.00.00.00", "X2016.07.14.00.00.00", "X2016.07.13.00.00.00",
"X2016.07.12.00.00.00", "X2016.07.11.00.00.00", "X2016.07.10.00.00.00",
"X2016.07.09.00.00.00", "X2016.07.08.00.00.00", "X2016.07.07.00.00.00",
"X2016.07.06.00.00.00", "X2016.07.05.00.00.00", "X2016.07.04.00.00.00",
"X2016.07.03.00.00.00", "X2016.07.02.00.00.00", "X2016.07.01.00.00.00",
"X2016.06.30.00.00.00", "X2016.06.29.00.00.00", "X2016.06.28.00.00.00",
"X2016.06.27.00.00.00", "X2016.06.26.00.00.00", "X2016.06.25.00.00.00",
"X2016.06.24.00.00.00", "X2016.06.23.00.00.00", "X2016.06.22.00.00.00",
"X2016.06.21.00.00.00", "X2016.06.20.00.00.00", "X2016.06.19.00.00.00",
"X2016.06.18.00.00.00", "X2016.06.17.00.00.00", "X2016.06.16.00.00.00",
"X2016.06.15.00.00.00", "X2016.06.14.00.00.00", "X2016.06.13.00.00.00",
"X2016.06.12.00.00.00", "X2016.06.11.00.00.00", "X2016.06.10.00.00.00",
"X2016.06.09.00.00.00", "X2016.06.08.00.00.00", "X2016.06.07.00.00.00",
"X2016.06.06.00.00.00", "X2016.06.05.00.00.00", "X2016.06.04.00.00.00",
"X2016.06.03.00.00.00", "X2016.06.02.00.00.00", "X2016.06.01.00.00.00",
"X2016.05.31.00.00.00", "X2016.05.30.00.00.00", "X2016.05.29.00.00.00",
"X2016.05.28.00.00.00", "X2016.05.27.00.00.00", "X2016.05.26.00.00.00",
"X2016.05.25.00.00.00", "X2016.05.24.00.00.00", "X2016.05.23.00.00.00",
"X2016.05.22.00.00.00", "X2016.05.21.00.00.00", "X2016.05.20.00.00.00",
"X2016.05.19.00.00.00", "X2016.05.18.00.00.00", "X2016.05.17.00.00.00",
"X2016.05.16.00.00.00", "X2016.05.15.00.00.00", "X2016.05.14.00.00.00",
"X2016.05.13.00.00.00", "X2016.05.12.00.00.00", "X2016.05.11.00.00.00",
"X2016.05.10.00.00.00", "X2016.05.09.00.00.00", "X2016.05.08.00.00.00",
"X2016.05.07.00.00.00", "X2016.05.06.00.00.00", "X2016.05.05.00.00.00",
"X2016.05.04.00.00.00", "X2016.05.03.00.00.00", "X2016.05.02.00.00.00",
"X2016.05.01.00.00.00", "X2016.04.30.00.00.00", "X2016.04.29.00.00.00",
"X2016.04.28.00.00.00", "X2016.04.27.00.00.00", "X2016.04.26.00.00.00",
"X2016.04.25.00.00.00", "X2016.04.24.00.00.00", "X2016.04.23.00.00.00",
"X2016.04.22.00.00.00", "X2016.04.21.00.00.00", "X2016.04.20.00.00.00",
"X2016.04.19.00.00.00", "X2016.04.18.00.00.00", "X2016.04.17.00.00.00",
"X2016.04.16.00.00.00", "X2016.04.15.00.00.00", "X2016.04.14.00.00.00",
"X2016.04.13.00.00.00", "X2016.04.12.00.00.00", "X2016.04.11.00.00.00",
"X2016.04.10.00.00.00", "X2016.04.09.00.00.00", "X2016.04.08.00.00.00",
"X2016.04.07.00.00.00", "X2016.04.06.00.00.00", "X2016.04.05.00.00.00",
"X2016.04.04.00.00.00", "X2016.04.03.00.00.00", "X2016.04.02.00.00.00",
"X2016.04.01.00.00.00", "X2016.03.31.00.00.00", "X2016.03.30.00.00.00",
"X2016.03.29.00.00.00", "X2016.03.28.00.00.00", "X2016.03.27.00.00.00",
"X2016.03.26.00.00.00", "X2016.03.25.00.00.00", "X2016.03.24.00.00.00",
"X2016.03.23.00.00.00", "X2016.03.22.00.00.00", "X2016.03.21.00.00.00",
"X2016.03.20.00.00.00", "X2016.03.19.00.00.00", "X2016.03.18.00.00.00",
"X2016.03.17.00.00.00", "X2016.03.16.00.00.00", "X2016.03.15.00.00.00",
"X2016.03.14.00.00.00", "X2016.03.13.00.00.00", "X2016.03.12.00.00.00",
"X2016.03.11.00.00.00", "X2016.03.10.00.00.00", "X2016.03.09.00.00.00",
"X2016.03.08.00.00.00", "X2016.03.07.00.00.00", "X2016.03.06.00.00.00",
"X2016.03.05.00.00.00", "X2016.03.04.00.00.00", "X2016.03.03.00.00.00",
"X2016.03.02.00.00.00", "X2016.03.01.00.00.00", "X2016.02.29.00.00.00",
"X2016.02.28.00.00.00", "X2016.02.27.00.00.00", "X2016.02.26.00.00.00",
"X2016.02.25.00.00.00", "X2016.02.24.00.00.00", "X2016.02.23.00.00.00",
"X2016.02.22.00.00.00", "X2016.02.21.00.00.00", "X2016.02.20.00.00.00",
"X2016.02.19.00.00.00", "X2016.02.18.00.00.00", "X2016.02.17.00.00.00",
"X2016.02.16.00.00.00", "X2016.02.15.00.00.00", "X2016.02.14.00.00.00",
"X2016.02.13.00.00.00", "X2016.02.12.00.00.00", "X2016.02.11.00.00.00",
"X2016.02.10.00.00.00", "X2016.02.09.00.00.00", "X2016.02.08.00.00.00",
"X2016.02.07.00.00.00", "X2016.02.06.00.00.00", "X2016.02.05.00.00.00",
"X2016.02.04.00.00.00", "X2016.02.03.00.00.00", "X2016.02.02.00.00.00",
"X2016.02.01.00.00.00", "X2016.01.31.00.00.00", "X2016.01.30.00.00.00",
"X2016.01.29.00.00.00", "X2016.01.28.00.00.00", "X2016.01.27.00.00.00",
"X2016.01.26.00.00.00", "X2016.01.25.00.00.00", "X2016.01.24.00.00.00",
"X2016.01.23.00.00.00", "X2016.01.22.00.00.00", "X2016.01.21.00.00.00",
"X2016.01.20.00.00.00", "X2016.01.19.00.00.00", "X2016.01.18.00.00.00",
"X2016.01.17.00.00.00", "X2016.01.16.00.00.00", "X2016.01.15.00.00.00",
"X2016.01.14.00.00.00", "X2016.01.13.00.00.00", "X2016.01.12.00.00.00",
"X2016.01.11.00.00.00", "X2016.01.10.00.00.00", "X2016.01.09.00.00.00",
"X2016.01.08.00.00.00", "X2016.01.07.00.00.00", "X2016.01.06.00.00.00",
"X2016.01.05.00.00.00", "X2016.01.04.00.00.00", "X2016.01.03.00.00.00",
"X2016.01.02.00.00.00", "X2016.01.01.00.00.00"), row.names = c(NA,
6L), class = "data.frame")
I haven't tested this, since I don't have your data files, so let me know if the code below gives you what you wanted. The steps are as follows:
Get the names of the three worksheets in the Excel workbook (I'm assuming they'll be something like "close", "low", and "high").
Read each worksheet into a list of data frames using lapply. The data in your worksheets is transposed (i.e., the variables are in rows instead of columns), so we we transpose the data so that each variable is in a column, and we also add an extra column that tells us which worksheet the data originally came from.
Combine the three data frames into a single data frame.
"melt" so that the three different currencies will be stacked in long format.
The final output, df, should be a data frame in "long" format that's ready for analysis.
library(reshape2)
library(XLConnect)
wbFX <- loadWorkbook("fx.xlsx")
# Get names of worksheets
sheets = getSheets(wbFX)
# Read the three worksheets into a list of data frames
df = lapply(sheets, function(sh) {
dat = as.data.frame(t(readWorksheet(wbFX, sheet=sh)))
dat$Date = as.Date(dat$Date)
dat$Price_Type = sh
dat
})
# Combine each list element into a single data frame
df = do.call(rbind, df)
# Melt to long format
df = melt(df, id.var=c("Date", "Price_Type"), variable.name="Currency", value.name="Price")
Maybe try
library(reshape2)
lst <- lapply(list(high, low, close), melt, id.vars = 1, variable.name = "var")
df <- Reduce(function(...) merge(..., by = c("Date", "var")), lst )
names(df) <- c("currency", "date", "high", "low", "close")
recast(df, currency+variable~date, id.var = 1:2)
which should give you something like
# currency variable 1/2/15 1/3/15 1/4/15
# 1 Euro high ... ... ...
# 2 Euro low ... ... ...
# 3 Euro close ... ... ...

Resources