How to attach dates to Time Series in R - r

I would like to create a time series of dissolved oxygen levels over time at three different depths. Currently, I have my dates as a column in excel. When I run the data set through autoplot() I end up with four graphs consisting of time, top, middle, and bottom. I would like to have the top, middle and bottom graphs with time on the x axis.
TomDO<- ts(Tom_Frost_Dissolved_Oxygen)
autoplot(TomDO, ts.geom = 'ribbon', fill = 'blue')
Data: Tom_Frost_Dissolved_Oxygen
Top Middle Bottom Date
12.55 13.39 9.55 3/9/15
6.8 6.55 0.36 3/31/15
7.22 6.64 6.01 4/13/15
5.94 5.78 5.58 4/29/15
7.01 6.41 6.29 5/11/15
6.76 5.96 4.07 5/26/15
3.22 2.68 1.8 6/8/15
6.08 5.88 3.44 6/23/15
5.02 4.34 4.25 7/20/15
3.07 2.6 2.3 8/3/15
3.9 3.62 3.23 8/17/15
8.97 8.53 6.54 8/31/15
6.96 5.94 5.06 9/14/15
3.87 3.78 2.81 9/28/15
4.1 3.99 3.9 10/12/15
5.04 4.91 4.77 10/26/15
8.77 8.61 8.6 3/8/16
9.12 9.22 9.09 3/22/16
8.78 8.87 8.6 4/4/16
7.78 7.67 6.9 5/2/16
5.83 5.3 4.78 5/31/16
4.56 4.52 4.46 6/14/16
6.6 6.02 0.28 6/27/16
10.82 10.02 4.31 7/11/16
6.79 5.05 3.61 7/25/16
6.45 4.78 3.83 8/8/16
3.27 2.6 2.57 8/22/16
5.3 5.16 5.15 9/6/16
5.66 4.74 4.23 9/19/16
4.79 4.65 4.47 10/3/16
7.27 7.23 6.75 10/17/16
6.15 6.05 5.89 10/31/16
6.96 6.73 6.42 11/14/16
10.19 10.16 9.93 3/9/17
7.66 7.48 7.16 3/20/17
6.67 6.46 6.46 4/3/17
7.04 6.82 5.88 4/17/17
7.94 7.83 7.85 5/1/17
5.56 5.43 2.12 5/15/17
4.15 3.99 3.67 5/30/17
6.41 6.06 4.3 6/12/17
8.83 8.07 6.74 6/26/17
8.57 7.68 7.37 7/10/17
11.62 9.2 5.27 7/24/17
13.58 11.69 9.15 8/21/17
9.6 9.54 9.12 9/5/17
8.71 7.46 6.6 9/18/17
6.13 5.75 4.97 10/5/17
6.36 5.93 5.37 10/16/17
6.88 6.89 6.74 11/13/17
11.61 10.82 10.09 3/7/18
6.47 6.62 6.17 4/2/18
7.14 7.1 7.04 4/16/18
8.33 8.12 6.36 4/30/18
5.79 5.25 4.3 5/29/18
13.28 11.39 7.01 6/14/18
7.82 7.34 5.92 6/25/18
11.12 9.08 8.21 7/9/18
7.68 3.8 2.68 7/23/18
6.55 5.24 4.04 8/6/18
2.53 2.18 2.08 8/20/18
11.63 11.38 9.76 9/4/18
6.02 5.83 5.17 9/17/18
9.66 9.28 8.73 10/1/18
8.01 7.95 7.91 10/29/18
9.31 8.88 1.43 5/13/19
6.66 6.02 5.8 5/29/19
5.31 4.82 3.9 6/10/19
1.9 1.13 0.16 7/8/19
2.05 1.4 0.28 7/11/19
6.72 5.49 2.57 7/22/19
13.65 4.29 3.81 8/5/19
0.69 0 0 8/19/19
12.8 7.5 2.33 8/22/19
3.83 3.45 3.08 9/4/19
7.57 5.36 3.77 9/16/19
9.33 9.02 8.41 9/30/19
11.63 11.58 11.45 10/14/19
9.82 9.65 8.89 11/14/19

Using the tidyverse collection of package, you can get the expected plot by 1) setting date as a date format, 2) then reshape your data into a longer format using pivot_longer and plot it using ggplot and facet_wrap:
library(tidyverse)
df$Date <- as.Date(df$Date, format = "%m/%d/%Y")
df %>% pivot_longer(., -Date, names_to = "Var",values_to = "Val") %>%
ggplot(aes(x = Date))+
geom_ribbon(aes(ymin = 0, ymax = Val), fill = "blue")+
facet_wrap(Var~., ncol = 1)+
scale_x_date(date_breaks = "3 months", date_labels = "%b %Y")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Does it look what you are looking for ?

Related

How can I subset my list to have only the last day of the month?

DGS1MO DGS3MO DGS1 DGS2 DGS3 DGS5 DGS7 DGS10 DGS20 DGS30
2001-07-31 3.67 3.54 3.53 3.79 4.06 4.57 4.86 5.07 5.61 5.51
2001-08-01 3.65 3.53 3.56 3.83 4.09 4.62 4.90 5.11 5.63 5.53
2001-08-02 3.65 3.53 3.57 3.89 4.17 4.69 4.97 5.17 5.68 5.57
2001-08-03 3.63 3.52 3.57 3.91 4.22 4.72 4.99 5.20 5.70 5.59
2001-08-06 3.62 3.52 3.56 3.88 4.17 4.71 4.99 5.19 5.70 5.59
2001-08-07 3.63 3.52 3.56 3.90 4.19 4.72 5.00 5.20 5.71 5.60
2001-08-08 3.61 3.49 3.46 3.77 4.05 4.61 4.87 4.99 5.61 5.52
2001-08-09 3.61 3.45 3.48 3.77 4.07 4.66 4.93 5.04 5.64 5.54
2001-08-10 3.58 3.43 3.45 3.73 4.03 4.61 4.88 4.99 5.61 5.52
2001-08-13 3.57 3.45 3.43 3.70 4.00 4.57 4.86 4.97 5.60 5.52
2001-08-14 3.54 3.43 3.46 3.74 4.03 4.59 4.87 4.97 5.61 5.51
2001-08-15 3.52 3.43 3.47 3.80 4.11 4.62 4.90 5.00 5.62 5.52
2001-08-16 3.48 3.39 3.43 3.75 4.04 4.58 4.84 4.95 5.58 5.48
2001-08-17 3.46 3.36 3.39 3.67 3.95 4.49 4.75 4.84 5.51 5.43
2001-08-20 3.48 3.42 3.44 3.74 4.02 4.55 4.81 4.91 5.55 5.46
2001-08-21 3.46 3.39 3.41 3.69 3.96 4.50 4.79 4.87 5.54 5.44
2001-08-22 3.46 3.38 3.44 3.76 4.03 4.53 4.81 4.91 5.53 5.44
2001-08-23 3.49 3.40 3.46 3.72 3.99 4.52 4.79 4.89 5.50 5.41
2001-08-24 3.49 3.42 3.48 3.76 4.03 4.55 4.82 4.93 5.54 5.45
2001-08-27 3.52 3.45 3.51 3.78 4.04 4.57 4.83 4.94 5.56 5.47
2001-08-28 3.53 3.41 3.46 3.71 3.97 4.48 4.73 4.85 5.49 5.41
2001-08-29 3.48 3.42 3.44 3.67 3.92 4.43 4.67 4.78 5.44 5.36
2001-08-30 3.41 3.36 3.38 3.61 3.88 4.42 4.68 4.79 5.45 5.37
2001-08-31 3.40 3.37 3.41 3.64 3.91 4.46 4.72 4.85 5.47 5.39
2001-09-04 3.43 3.44 3.55 3.83 4.10 4.63 4.88 4.99 5.59 5.50
2001-09-05 3.49 3.41 3.47 3.79 4.07 4.61 4.86 4.97 5.57 5.48
2001-09-06 3.44 3.34 3.40 3.65 3.93 4.48 4.73 4.86 5.50 5.41
2001-09-07 3.40 3.27 3.29 3.53 3.82 4.39 4.67 4.80 5.45 5.39
2001-09-10 3.40 3.26 3.31 3.53 3.82 4.41 4.69 4.84 5.50 5.43
2001-09-13 2.73 2.74 2.81 2.99 3.32 4.03 4.41 4.64 5.41 5.39
2001-09-14 2.54 2.64 2.73 2.87 3.17 3.92 4.31 4.57 5.38 5.35
2001-09-17 2.47 2.59 2.72 2.96 3.30 3.99 4.38 4.63 5.44 5.41
2001-09-18 2.34 2.48 2.69 2.96 3.31 4.01 4.46 4.72 5.59 5.55
2001-09-19 2.00 2.19 2.49 2.81 3.18 3.90 4.41 4.69 5.59 5.56
2001-09-20 2.04 2.22 2.56 2.91 3.27 3.97 4.47 4.75 5.67 5.62
2001-09-21 2.12 2.25 2.53 2.91 3.27 3.94 4.43 4.70 5.62 5.59
2001-09-24 2.38 2.38 2.56 2.94 3.30 4.00 4.47 4.73 5.61 5.58
2001-09-25 2.58 2.40 2.51 2.88 3.25 3.97 4.45 4.72 5.60 5.58
2001-09-26 2.51 2.38 2.48 2.82 3.18 3.91 4.39 4.65 5.52 5.50
2001-09-27 2.34 2.38 2.43 2.78 3.15 3.87 4.33 4.58 5.46 5.45
2001-09-28 2.28 2.40 2.49 2.86 3.22 3.93 4.37 4.60 5.45 5.42
2001-10-01 2.26 2.37 2.47 2.82 3.18 3.90 4.33 4.55 5.39 5.38
2001-10-02 2.27 2.26 2.43 2.77 3.14 3.87 4.31 4.53 5.36 5.34
2001-10-03 2.21 2.23 2.38 2.77 3.14 3.86 4.29 4.50 5.34 5.32
2001-10-04 2.22 2.21 2.37 2.75 3.14 3.88 4.29 4.53 5.33 5.31
2001-10-05 2.21 2.19 2.33 2.71 3.10 3.87 4.26 4.52 5.34 5.31
2001-10-09 2.24 2.22 2.35 2.74 3.16 3.96 4.35 4.62 5.42 5.39
Above I have my dataset that I am trying to subset. My goals is to subset the df to only include the last day of each month listed. For example 8/31/2021,9/27/2021...etc onward through the data.
I have been able to do specific dates but I need something that is dynamic.
Thanks in advance
With lubridate:
library(lubridate)
df$DAY <- as.Date(df$DAY)
df[df$DAY == ceiling_date(df$DAY,'month') - days(1),]
DAY DGS1MO DGS3MO DGS1 DGS2 DGS3 DGS5 DGS7 DGS10 DGS20 DGS30
1 2001-07-31 3.67 3.54 3.53 3.79 4.06 4.57 4.86 5.07 5.61 5.51
24 2001-08-31 3.40 3.37 3.41 3.64 3.91 4.46 4.72 4.85 5.47 5.39
df:
df <- read.table(text='
DAY DGS1MO DGS3MO DGS1 DGS2 DGS3 DGS5 DGS7 DGS10 DGS20 DGS30
2001-07-31 3.67 3.54 3.53 3.79 4.06 4.57 4.86 5.07 5.61 5.51
2001-08-01 3.65 3.53 3.56 3.83 4.09 4.62 4.90 5.11 5.63 5.53
2001-08-02 3.65 3.53 3.57 3.89 4.17 4.69 4.97 5.17 5.68 5.57
2001-08-03 3.63 3.52 3.57 3.91 4.22 4.72 4.99 5.20 5.70 5.59
2001-08-06 3.62 3.52 3.56 3.88 4.17 4.71 4.99 5.19 5.70 5.59
2001-08-07 3.63 3.52 3.56 3.90 4.19 4.72 5.00 5.20 5.71 5.60
2001-08-08 3.61 3.49 3.46 3.77 4.05 4.61 4.87 4.99 5.61 5.52
2001-08-09 3.61 3.45 3.48 3.77 4.07 4.66 4.93 5.04 5.64 5.54
2001-08-10 3.58 3.43 3.45 3.73 4.03 4.61 4.88 4.99 5.61 5.52
2001-08-13 3.57 3.45 3.43 3.70 4.00 4.57 4.86 4.97 5.60 5.52
2001-08-14 3.54 3.43 3.46 3.74 4.03 4.59 4.87 4.97 5.61 5.51
2001-08-15 3.52 3.43 3.47 3.80 4.11 4.62 4.90 5.00 5.62 5.52
2001-08-16 3.48 3.39 3.43 3.75 4.04 4.58 4.84 4.95 5.58 5.48
2001-08-17 3.46 3.36 3.39 3.67 3.95 4.49 4.75 4.84 5.51 5.43
2001-08-20 3.48 3.42 3.44 3.74 4.02 4.55 4.81 4.91 5.55 5.46
2001-08-21 3.46 3.39 3.41 3.69 3.96 4.50 4.79 4.87 5.54 5.44
2001-08-22 3.46 3.38 3.44 3.76 4.03 4.53 4.81 4.91 5.53 5.44
2001-08-23 3.49 3.40 3.46 3.72 3.99 4.52 4.79 4.89 5.50 5.41
2001-08-24 3.49 3.42 3.48 3.76 4.03 4.55 4.82 4.93 5.54 5.45
2001-08-27 3.52 3.45 3.51 3.78 4.04 4.57 4.83 4.94 5.56 5.47
2001-08-28 3.53 3.41 3.46 3.71 3.97 4.48 4.73 4.85 5.49 5.41
2001-08-29 3.48 3.42 3.44 3.67 3.92 4.43 4.67 4.78 5.44 5.36
2001-08-30 3.41 3.36 3.38 3.61 3.88 4.42 4.68 4.79 5.45 5.37
2001-08-31 3.40 3.37 3.41 3.64 3.91 4.46 4.72 4.85 5.47 5.39
2001-09-04 3.43 3.44 3.55 3.83 4.10 4.63 4.88 4.99 5.59 5.50
2001-09-05 3.49 3.41 3.47 3.79 4.07 4.61 4.86 4.97 5.57 5.48
2001-09-06 3.44 3.34 3.40 3.65 3.93 4.48 4.73 4.86 5.50 5.41
2001-09-07 3.40 3.27 3.29 3.53 3.82 4.39 4.67 4.80 5.45 5.39
2001-09-10 3.40 3.26 3.31 3.53 3.82 4.41 4.69 4.84 5.50 5.43
2001-09-13 2.73 2.74 2.81 2.99 3.32 4.03 4.41 4.64 5.41 5.39
2001-09-14 2.54 2.64 2.73 2.87 3.17 3.92 4.31 4.57 5.38 5.35
2001-09-17 2.47 2.59 2.72 2.96 3.30 3.99 4.38 4.63 5.44 5.41
2001-09-18 2.34 2.48 2.69 2.96 3.31 4.01 4.46 4.72 5.59 5.55
2001-09-19 2.00 2.19 2.49 2.81 3.18 3.90 4.41 4.69 5.59 5.56
2001-09-20 2.04 2.22 2.56 2.91 3.27 3.97 4.47 4.75 5.67 5.62
2001-09-21 2.12 2.25 2.53 2.91 3.27 3.94 4.43 4.70 5.62 5.59
2001-09-24 2.38 2.38 2.56 2.94 3.30 4.00 4.47 4.73 5.61 5.58
2001-09-25 2.58 2.40 2.51 2.88 3.25 3.97 4.45 4.72 5.60 5.58
2001-09-26 2.51 2.38 2.48 2.82 3.18 3.91 4.39 4.65 5.52 5.50
2001-09-27 2.34 2.38 2.43 2.78 3.15 3.87 4.33 4.58 5.46 5.45
2001-09-28 2.28 2.40 2.49 2.86 3.22 3.93 4.37 4.60 5.45 5.42
2001-10-01 2.26 2.37 2.47 2.82 3.18 3.90 4.33 4.55 5.39 5.38
2001-10-02 2.27 2.26 2.43 2.77 3.14 3.87 4.31 4.53 5.36 5.34
2001-10-03 2.21 2.23 2.38 2.77 3.14 3.86 4.29 4.50 5.34 5.32
2001-10-04 2.22 2.21 2.37 2.75 3.14 3.88 4.29 4.53 5.33 5.31
2001-10-05 2.21 2.19 2.33 2.71 3.10 3.87 4.26 4.52 5.34 5.31
2001-10-09 2.24 2.22 2.35 2.74 3.16 3.96 4.35 4.62 5.42 5.39',header=T)
A possible solution:
library(tidyverse)
library(lubridate)
df %>%
mutate(d = day(ymd(Date)), m = month(ymd(Date)), y = year(ymd(Date))) %>%
group_by(m, y) %>%
slice_max(d) %>%
ungroup %>%
select(-d, -m, -y)
#> # A tibble: 4 × 11
#> Date DGS1MO DGS3MO DGS1 DGS2 DGS3 DGS5 DGS7 DGS10 DGS20 DGS30
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2001-07-31 3.67 3.54 3.53 3.79 4.06 4.57 4.86 5.07 5.61 5.51
#> 2 2001-08-31 3.4 3.37 3.41 3.64 3.91 4.46 4.72 4.85 5.47 5.39
#> 3 2001-09-28 2.28 2.4 2.49 2.86 3.22 3.93 4.37 4.6 5.45 5.42
#> 4 2001-10-09 2.24 2.22 2.35 2.74 3.16 3.96 4.35 4.62 5.42 5.39
A date is the last of a month if and only if it is one day prior to the first of the following month. You can index the elements of a Date vector x satisfying this condition like so:
is_last_of_month <- function(x) {
x <- trunc(x)
x == as.Date(round(as.POSIXlt(x), units = "months")) - 1
}
x <- seq(as.Date("2022-02-24"), as.Date("2022-03-05"), by = 1)
x
## [1] "2022-02-24" "2022-02-25" "2022-02-26" "2022-02-27" "2022-02-28"
## [6] "2022-03-01" "2022-03-02" "2022-03-03" "2022-03-04" "2022-03-05"
is_last_of_month(x)
## [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
is_last_of_month operates on trunc(x) instead of x to defend against the possibility of fractional days, so that—for example—is_last_of_month(as.Date("2022-02-28") + u) is TRUE for all u greater than 0 but less than 1:
y <- as.Date("2022-02-28") + 0.5
y
## [1] "2022-02-28"
y == as.Date(round(as.POSIXlt(y), units = "months")) - 1
## [1] FALSE
is_last_of_month(y)
## [1] TRUE
(Well, "... for all u greater than 0 but less than 1" is not quite true in floating point arithmetic, but hopefully my meaning is clear.)

How to define a function in a for loop with an incremental variable in R?

I am trying to write a function that calculates the total stress, σ_T, using the incremental function below.
x is the input data from a text file. τ and K are unknown values and dt is a known constant.
The equation uses the output at each step, σ_T, in the equation for the next step. I previously defined this function in MATLAB using a for loop, but I have read this is not advised for R. I looked into apply, but I am not sure how to implement it with my incremental function.
The ultimate goal is to use this function in nonlinear regression analysis.
I have included sample data below my question. It includes a column titled yT, which is the output for σ_T when K=773 and τ=4.576.
Added:
I tried using a for loop to define the equation, but I don't think it is correct. When I pass it to the regression analysis I get an error. I also changed the equation slightly from the original image.
yT <- function(x, tau, k){
for(i in seq(from=1, to=200, by=1)){
if (i == 1) {
yO = 0
dx = 0
} else {
yO = yT
dx = x[i] - x[i-1] }
yT = exp(-0.02/tau)*yO + ((k/2) * (1 + exp(-0.02/tau)*dx))
} }
time x yT
0.00 0.000E+00 0.00000
0.02 0.000E+00 0.00000
0.04 1.000E-05 0.00771
0.06 1.420E-05 0.01863
0.08 -1.800E-06 0.01716
0.10 3.000E-06 0.01940
0.12 2.080E-05 0.03536
0.14 2.410E-05 0.05379
0.16 8.000E-06 0.05973
0.18 -5.100E-06 0.05554
0.20 7.300E-06 0.06093
0.22 2.750E-05 0.08187
0.24 2.560E-05 0.10126
0.26 6.800E-06 0.10606
0.28 -1.100E-06 0.10475
0.30 1.350E-05 0.11471
0.32 2.670E-05 0.13480
0.34 1.520E-05 0.14594
0.36 -6.000E-06 0.14067
0.38 -5.700E-06 0.13566
0.40 1.200E-05 0.14433
0.42 1.570E-05 0.15581
0.44 3.400E-06 0.15775
0.46 -3.800E-06 0.15413
0.48 7.300E-06 0.15909
0.50 1.970E-05 0.17359
0.52 1.530E-05 0.18464
0.54 7.000E-07 0.18437
0.56 -3.200E-06 0.18110
0.58 1.200E-05 0.18956
0.60 2.220E-05 0.20586
0.62 1.480E-05 0.21638
0.64 2.600E-06 0.21744
0.66 3.900E-06 0.21950
0.68 1.740E-05 0.23196
0.70 2.180E-05 0.24777
0.72 8.800E-06 0.25347
0.74 -4.200E-06 0.24913
0.76 5.600E-06 0.25236
0.78 2.200E-05 0.26823
0.80 2.000E-05 0.28249
0.82 6.000E-06 0.28588
0.84 7.000E-06 0.29004
0.86 1.700E-05 0.30188
0.88 1.800E-05 0.31445
0.90 2.000E-06 0.31462
0.92 -2.000E-06 0.31171
0.94 1.400E-05 0.32115
0.96 2.200E-05 0.33671
0.98 1.200E-05 0.34450
1.00 6.000E-06 0.34763
1.02 1.500E-05 0.35768
1.04 2.200E-05 0.37309
1.06 1.200E-05 0.38072
1.08 0.000E+00 0.37906
1.10 8.000E-06 0.38358
1.12 2.100E-05 0.39810
1.14 1.900E-05 0.41102
1.16 8.000E-06 0.41540
1.18 1.000E-05 0.42130
1.20 2.200E-05 0.43643
1.22 2.000E-05 0.44995
1.24 4.000E-06 0.45108
1.26 1.000E-06 0.44988
1.28 1.800E-05 0.46180
1.30 2.300E-05 0.47753
1.32 1.200E-05 0.48470
1.34 8.000E-06 0.48876
1.36 1.800E-05 0.50051
1.38 2.400E-05 0.51684
1.40 1.200E-05 0.52384
1.42 0.000E+00 0.52156
1.44 1.100E-05 0.52777
1.46 2.600E-05 0.54552
1.48 1.900E-05 0.55780
1.50 8.000E-06 0.56154
1.52 1.300E-05 0.56911
1.54 2.600E-05 0.58669
1.56 2.100E-05 0.60033
1.58 3.000E-06 0.60002
1.60 4.000E-06 0.60049
1.62 2.200E-05 0.61484
1.64 2.500E-05 0.63144
1.66 1.400E-05 0.63949
1.68 9.000E-06 0.64364
1.70 2.200E-05 0.65780
1.72 2.500E-05 0.67422
1.74 1.100E-05 0.67976
1.76 2.000E-06 0.67834
1.78 1.400E-05 0.68618
1.80 2.800E-05 0.70478
1.82 1.900E-05 0.71637
1.84 9.000E-06 0.72018
1.86 1.600E-05 0.72938
1.88 2.800E-05 0.74780
1.90 2.100E-05 0.76074
1.92 3.000E-06 0.75973
1.94 6.000E-06 0.76105
1.96 2.500E-05 0.77701
1.98 2.600E-05 0.79368
2.00 1.300E-05 0.80024
2.02 1.200E-05 0.80601
2.04 2.600E-05 0.82255
2.06 2.800E-05 0.84056
2.08 1.100E-05 0.84538
2.10 2.000E-06 0.84323
2.12 1.800E-05 0.85344
2.14 3.000E-05 0.87286
2.16 2.000E-05 0.88448
2.18 1.000E-05 0.88833
2.20 2.100E-05 0.90066
2.22 3.100E-05 0.92064
2.24 2.000E-05 0.93205
2.26 3.000E-06 0.93030
2.28 1.000E-05 0.93396
2.30 2.700E-05 0.95071
2.32 2.500E-05 0.96585
2.34 1.200E-05 0.97089
2.36 1.500E-05 0.97823
2.38 2.900E-05 0.99633
2.40 2.700E-05 1.01281
2.42 1.000E-05 1.01611
2.44 5.000E-06 1.01553
2.46 2.000E-05 1.02653
2.48 2.900E-05 1.04442
2.50 2.000E-05 1.05529
2.52 1.200E-05 1.05995
2.54 2.400E-05 1.07384
2.56 3.200E-05 1.09384
2.58 1.800E-05 1.10295
2.60 4.000E-06 1.10122
2.62 1.300E-05 1.10645
2.64 2.900E-05 1.12399
2.66 2.400E-05 1.13760
2.68 1.400E-05 1.14344
2.70 1.700E-05 1.15157
2.72 3.000E-05 1.16968
2.74 2.600E-05 1.18464
2.76 7.000E-06 1.18487
2.78 7.000E-06 1.18510
2.80 2.400E-05 1.19845
2.82 2.900E-05 1.21559
2.84 1.700E-05 1.22340
2.86 1.300E-05 1.22809
2.88 2.500E-05 1.24202
2.90 3.200E-05 1.26128
2.92 1.600E-05 1.26812
2.94 5.000E-06 1.26645
2.96 1.600E-05 1.27327
2.98 2.800E-05 1.28931
3.00 2.100E-05 1.29989
3.02 1.500E-05 1.30579
3.04 2.000E-05 1.31552
3.06 2.800E-05 1.33138
3.08 2.200E-05 1.34254
3.10 9.000E-06 1.34363
3.12 1.100E-05 1.34626
3.14 2.400E-05 1.35890
3.16 2.600E-05 1.37303
3.18 1.600E-05 1.37938
3.20 1.400E-05 1.38416
3.22 2.700E-05 1.39895
3.24 2.700E-05 1.41368
3.26 1.200E-05 1.41677
3.28 4.000E-06 1.41367
3.30 1.900E-05 1.42217
3.32 2.900E-05 1.43833
3.34 2.100E-05 1.44826
3.36 1.600E-05 1.45428
3.38 2.200E-05 1.46491
3.40 3.000E-05 1.48166
3.42 1.900E-05 1.48985
3.44 6.000E-06 1.48798
3.46 1.300E-05 1.49152
3.48 2.600E-05 1.50507
3.50 2.400E-05 1.51702
3.52 1.600E-05 1.52275
3.54 1.900E-05 1.53076
3.56 2.900E-05 1.54645
3.58 2.400E-05 1.55822
3.60 1.400E-05 1.56222
3.62 1.800E-05 1.56930
3.64 2.600E-05 1.58251
3.66 1.700E-05 1.58872
3.68 9.000E-06 1.58873
3.70 2.100E-05 1.59800
3.72 2.800E-05 1.61263
3.74 1.700E-05 1.61871
3.76 1.400E-05 1.62245
3.78 2.500E-05 1.63466
3.80 2.700E-05 1.64835
3.82 1.300E-05 1.65119
3.84 1.500E-05 1.65556
3.86 2.600E-05 1.66840
3.88 2.300E-05 1.67886
3.90 1.100E-05 1.68002
3.92 1.700E-05 1.68581
3.94 2.800E-05 1.70005
3.96 2.100E-05 1.70884
3.98 1.100E-05 1.70987
4.00 2.100E-05 1.71861
4.02 2.700E-05 1.73194
4.04 1.700E-05 1.73750
4.06 1.200E-05 1.73918
4.08 2.400E-05 1.75011
4.10 2.700E-05 1.76330
4.12 1.400E-05 1.76641
4.14 1.400E-05 1.76951
4.16 2.700E-05 1.78262
4.18 2.400E-05 1.79335
4.20 1.100E-05 1.79402
4.22 1.900E-05 1.80085
4.24 2.900E-05 1.81536
4.26 2.000E-05 1.82287
4.28 1.100E-05 1.82341
4.30 2.200E-05 1.83243
4.32 2.800E-05 1.84603
4.34 1.700E-05 1.85109
4.36 1.300E-05 1.85305
4.38 2.500E-05 1.86425
4.40 2.700E-05 1.87695
4.42 1.500E-05 1.88033
4.44 1.400E-05 1.88293
4.46 2.600E-05 1.89477
4.48 2.300E-05 1.90425
4.50 1.300E-05 1.90597
4.52 1.800E-05 1.91155
4.54 2.900E-05 1.92558
4.56 2.000E-05 1.93261
4.58 1.200E-05 1.93343
4.60 2.100E-05 1.94120
4.62 2.800E-05 1.95433
4.64 1.700E-05 1.95892
4.66 1.300E-05 1.96041
4.68 2.400E-05 1.97037
4.70 2.700E-05 1.98260
4.72 1.500E-05 1.98553
4.74 1.500E-05 1.98844
4.76 2.700E-05 2.00059
4.78 2.400E-05 2.01038
4.80 1.200E-05 2.01087
4.82 1.800E-05 2.01598
4.84 2.900E-05 2.02956
4.86 2.000E-05 2.03613
4.88 1.200E-05 2.03651
4.90 2.100E-05 2.04383
4.92 2.800E-05 2.05651
4.94 1.700E-05 2.06066
4.96 1.300E-05 2.06170
4.98 2.600E-05 2.07276
5.00 2.500E-05 2.08300
5.02 1.400E-05 2.08472
5.04 1.600E-05 2.08797
5.06 2.800E-05 2.10046
5.08 2.400E-05 2.10981
5.10 1.200E-05 2.10987
5.12 1.900E-05 2.11532
5.14 2.800E-05 2.12769
5.16 2.100E-05 2.13461
5.18 1.300E-05 2.13533
5.20 2.200E-05 2.14299
5.22 2.900E-05 2.15601
5.24 1.800E-05 2.16049
5.26 1.400E-05 2.16187
5.28 2.400E-05 2.17095
5.30 2.700E-05 2.18231
5.32 1.500E-05 2.18437
5.34 1.400E-05 2.18564
5.36 2.700E-05 2.19693
5.38 2.500E-05 2.20663
5.40 1.400E-05 2.20781
5.42 1.900E-05 2.21284
5.44 2.900E-05 2.22556
5.46 2.000E-05 2.23128
5.48 1.300E-05 2.23157
5.50 2.200E-05 2.23881
5.52 2.700E-05 2.24987
5.54 1.700E-05 2.25317
5.56 1.300E-05 2.25338
5.58 2.500E-05 2.26283
5.60 1.800E-05 2.26685
5.62 -1.200E-05 2.24771
5.64 -2.800E-05 2.21631
5.66 -4.000E-06 2.20356
5.68 2.000E-05 2.20937
5.70 1.100E-05 2.20822
5.72 -8.000E-06 2.19242
5.74 -1.200E-05 2.17361
5.76 -1.000E-06 2.16336
5.78 6.000E-06 2.15855
5.80 4.000E-06 2.15222
5.82 2.000E-06 2.14438
5.84 3.000E-06 2.13734
5.86 4.000E-06 2.13111
5.88 3.000E-06 2.12413
5.90 4.000E-06 2.11795
5.92 4.000E-06 2.11180
5.94 3.000E-06 2.10490
5.96 -3.000E-06 2.09341
5.98 -1.300E-05 2.07425
6.00 -1.200E-05 2.05595
6.02 0.000E+00 2.04699
6.04 5.000E-06 2.04192
6.06 3.000E-06 2.03533
6.08 3.000E-06 2.02877
6.10 3.000E-06 2.02223
6.12 2.000E-06 2.01496
6.14 2.000E-06 2.00771
6.16 4.000E-06 2.00204
6.18 4.000E-06 1.99640
6.20 3.000E-06 1.99000
6.22 2.000E-06 1.98287
6.24 -8.000E-06 1.96805
6.26 -1.500E-05 1.94790
6.28 -8.000E-06 1.93323
6.30 5.000E-06 1.92866
6.32 4.000E-06 1.92334
6.34 1.000E-06 1.91572
6.36 2.000E-06 1.90891
6.38 3.000E-06 1.90290
6.40 3.000E-06 1.89691
6.42 2.000E-06 1.89018
6.44 3.000E-06 1.88425
6.46 3.000E-06 1.87835
6.48 3.000E-06 1.87247
6.50 4.000E-06 1.86739
6.52 -1.000E-06 1.85848
6.54 -1.200E-05 1.84112
6.56 -1.300E-05 1.82306
6.58 -2.000E-06 1.81357
6.60 5.000E-06 1.80952
6.62 2.000E-06 1.80317
6.64 1.000E-06 1.79608
6.66 3.000E-06 1.79056
6.68 2.000E-06 1.78429
6.70 2.000E-06 1.77806
6.72 1.000E-06 1.77107
6.74 2.000E-06 1.76489
6.76 4.000E-06 1.76028
6.78 3.000E-06 1.75492
6.80 3.000E-06 1.74958
6.82 2.000E-06 1.74349
6.84 -5.000E-06 1.73203
6.86 -1.400E-05 1.71368
6.88 -1.000E-05 1.69850
6.90 3.000E-06 1.69340
6.92 5.000E-06 1.68987
6.94 1.000E-06 1.68328
6.96 1.000E-06 1.67671
6.98 1.000E-06 1.67017
7.00 2.000E-06 1.66443
7.02 4.000E-06 1.66025
7.04 3.000E-06 1.65533
7.06 2.000E-06 1.64965
7.08 2.000E-06 1.64400
7.10 2.000E-06 1.63837
7.12 2.000E-06 1.63277
7.14 2.000E-06 1.62719
7.16 0.000E+00 1.62010
7.18 -9.000E-06 1.60609
7.20 -1.400E-05 1.58829
7.22 -5.000E-06 1.57751
7.24 5.000E-06 1.57448
7.26 3.000E-06 1.56993
7.28 0.000E+00 1.56308
7.30 2.000E-06 1.55781
7.32 1.000E-06 1.55179
7.34 2.000E-06 1.54656
7.36 2.000E-06 1.54136
7.38 2.000E-06 1.53618
7.40 2.000E-06 1.53103
7.42 2.000E-06 1.52589
7.44 2.000E-06 1.52078
7.46 2.000E-06 1.51569
7.48 2.000E-06 1.51062
7.50 1.000E-06 1.50481
7.52 2.000E-06 1.49979
7.54 -3.000E-06 1.49093
7.56 -1.200E-05 1.47518
7.58 -1.200E-05 1.45949
7.60 1.000E-06 1.45390
7.62 4.000E-06 1.45064
7.64 1.000E-06 1.44509
7.66 1.000E-06 1.43955
7.68 1.000E-06 1.43405
7.70 1.000E-06 1.42857
7.72 1.000E-06 1.42311
7.74 2.000E-06 1.41844
7.76 3.000E-06 1.41457
7.78 3.000E-06 1.41072
7.80 1.000E-06 1.40534
7.82 1.000E-06 1.39998
7.84 2.000E-06 1.39542
7.86 1.000E-06 1.39010
7.88 3.000E-06 1.38636
7.90 2.000E-06 1.38185
7.92 1.000E-06 1.37660
7.94 0.000E+00 1.37059
7.96 -7.000E-06 1.35922
7.98 -1.400E-05 1.34249
8.00 -8.000E-06 1.33047
8.02 3.000E-06 1.32698
8.04 3.000E-06 1.32351
8.06 1.000E-06 1.31851
8.08 1.000E-06 1.31353
8.10 1.000E-06 1.30857
8.12 1.000E-06 1.30364
8.14 2.000E-06 1.29949
8.16 1.000E-06 1.29460
8.18 1.000E-06 1.28972
8.20 2.000E-06 1.28564
8.22 1.000E-06 1.28081
8.24 2.000E-06 1.27676
8.26 1.000E-06 1.27197
8.28 2.000E-06 1.26796
8.30 1.000E-06 1.26321
8.32 1.000E-06 1.25847
8.34 2.000E-06 1.25452
8.36 0.000E+00 1.24905
8.38 2.000E-06 1.24515
8.40 2.000E-06 1.24126
8.42 0.000E+00 1.23585
8.44 -7.000E-06 1.22506
8.46 -1.600E-05 1.20738
8.48 -5.000E-06 1.19825
8.50 4.000E-06 1.19611
8.52 3.000E-06 1.19321
8.54 -1.000E-06 1.18724
8.56 1.000E-06 1.18283
8.58 1.000E-06 1.17844
8.60 1.000E-06 1.17408
8.62 1.000E-06 1.16973
8.64 1.000E-06 1.16540
8.66 2.000E-06 1.16186
8.68 1.000E-06 1.15756
8.70 1.000E-06 1.15329
8.72 0.000E+00 1.14826
8.74 1.000E-06 1.14402
8.76 1.000E-06 1.13980
8.78 2.000E-06 1.13638
8.80 1.000E-06 1.13219
8.82 1.000E-06 1.12803
8.84 1.000E-06 1.12388
8.86 1.000E-06 1.11975
8.88 2.000E-06 1.11641
8.90 1.000E-06 1.11231
8.92 1.000E-06 1.10823
8.94 1.000E-06 1.10417
8.96 1.000E-06 1.10013
8.98 1.000E-06 1.09610
9.00 -5.000E-06 1.08746
9.02 -1.400E-05 1.07192
9.04 -1.000E-05 1.05954
9.06 2.000E-06 1.05646
9.08 4.000E-06 1.05494
9.10 0.000E+00 1.05034
9.12 0.000E+00 1.04575
9.14 1.000E-06 1.04197
9.16 0.000E+00 1.03742
9.18 1.000E-06 1.03367
9.20 1.000E-06 1.02993
9.22 0.000E+00 1.02544
9.24 1.000E-06 1.02174
9.26 1.000E-06 1.01806
9.28 1.000E-06 1.01439
9.30 1.000E-06 1.01074
9.32 2.000E-06 1.00787
9.34 0.000E+00 1.00348
9.36 1.000E-06 0.99987
9.38 2.000E-06 0.99705
9.40 2.000E-06 0.99425
9.42 1.000E-06 0.99068
9.44 1.000E-06 0.98714
9.46 1.000E-06 0.98360
9.48 0.000E+00 0.97931
9.50 0.000E+00 0.97504
9.52 1.000E-06 0.97156
9.54 1.000E-06 0.96810
9.56 0.000E+00 0.96387
9.58 1.000E-06 0.96044
9.60 1.000E-06 0.95702
9.62 1.000E-06 0.95362
9.64 1.000E-06 0.95024
9.66 1.000E-06 0.94686
9.68 1.000E-06 0.94350
9.70 1.000E-06 0.94016
9.72 -4.000E-06 0.93298
9.74 -1.300E-05 0.91888
9.76 -1.100E-05 0.90639
9.78 0.000E+00 0.90244
9.80 3.000E-06 0.90082
9.82 0.000E+00 0.89689
9.84 0.000E+00 0.89298
9.86 0.000E+00 0.88908
9.88 0.000E+00 0.88520
9.90 1.000E-06 0.88212
9.92 1.000E-06 0.87904
9.94 0.000E+00 0.87521
9.96 0.000E+00 0.87139
9.98 2.000E-06 0.86913
10.00 0.000E+00 0.86534
How about recursion?
sigma.T <- function(x,tao,kay,dt,n) {
yT = exp(-dt/tao) + ((kay/2) * (1 + exp(-dt/tao)*x))
if (n == 1) return (yT)
else return (sigma.T(x=x,tao=tao,kay=kay,dt=dt,n=n-1) * yT)
}
sigma.T(x=0,tao=4.576,kay=773,dt=0.01,n=10)
Not using your formula but just showing how you can calculate a new variable (column) based on previous values by using the lag function. In this simple case I calculate value to be the sum of x the current time stamp and yT of the previous time stamp. Off course, the first value will be NA as there is no value of yT before the time = 0.
library(dplyr)
library(tibble)
set.seed(123)
data <- tibble(
time = seq(0, 5, by = 0.02),
x = runif(-10, 10, n = 251),
yT = runif(0, 2, n = 251)
)
data2 <- data %>%
mutate(value = x + lag(yT, 1))
In base-R the solution is as simple:
data$value <- data$x + lag(data$yT)
There is no need for an explicit for loop as many R functions are vectorized: at the C-level the function loops through the vector elements. C-code is usually faster than R code.
My recommendation is if you can vectorize: do it. Apply and map functions can result in faster code than a for loop but you really need to understand the effects of immutable objects in R to do a proper assessement.
My opinion is that apply & map functions are more elegant and allow for better readable code and safer looping (you need to worry about indexing and exit conditions). When your current loop relies on a result from a previous loop I think explicit for loops are required.

R, too much data in data.frame

I have problem with data in R. I'm loading data with:
data<-read.csv2("ceny_paliwo.csv", dec = ",")
data
an this is giving me:
X Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec
1 2014 5.32 5.34 5.34 5.27 5.29 5.23 5.29 5.22 5.19 5.17 4.98 4.75
2 2015 4.46 4.47 4.62 4.58 4.65 4.71 4.66 4.49 4.30 4.28 4.36 4.21
3 2016 3.87 3.73 3.86 3.90 4.07 4.23 4.17 4.10 4.26 4.35 4.32 4.53
4 2017 4.62 4.58 4.53 4.48 4.36 4.19 4.17 4.31 4.37 4.44 4.59 4.59
after this:
data2 <- round(unname(unlist(as.data.frame(data))), digits = 2)
data2
I'm receiving:
[1] 2014.00 2015.00 2016.00 2017.00 5.32 4.46 3.87 4.62 5.34 4.47 3.73 4.58 5.34
[14] 4.62 3.86 4.53 5.27 4.58 3.90 4.48 5.29 4.65 4.07 4.36 5.23 4.71
[27] 4.23 4.19 5.29 4.66 4.17 4.17 5.22 4.49 4.10 4.31 5.19 4.30 4.26
[40] 4.37 5.17 4.28 4.35 4.44 4.98 4.36 4.32 4.59 4.75 4.21 4.53 4.59
What I'm trying to do, is to don't have 2014.00 2015.00 2016.00 2017.00 this data in the first row.
Any idea how to do it?
Select only data from the second column like here:
data2 <- round(unname(unlist(as.data.frame(data[,c(2:ncol(data))]))), digits = 2)

Assign columns to grouping variable for use with ordihull plotting

I have a dataframe consisting of dissimilarity ratings for pairs of 12 nations, and what I wish to do is essentially divide the columns (the nations) into three different groups (not combining their scores).
I am performing a non-metric multidimensional scaling, so I would like to plot the dissimilarity ratings with convex hulls according to these three groups.
I know the code for making the plots, all I am missing is the grouping variable that is needed, and I cannot for the life of me figure out how to create it.
Brazil Congo Cuba Egypt France India Israel Japan China UdSSR USA Yugoslavia
1 0.00 4.83 5.28 3.44 4.72 4.50 3.83 3.50 2.39 3.06 5.39 3.17
2 4.83 0.00 4.56 5.00 4.00 4.83 3.33 3.39 4.00 3.39 2.39 3.50
3 5.28 4.56 0.00 5.17 4.11 4.00 3.61 2.94 5.50 5.44 3.17 5.11
4 3.44 5.00 5.17 0.00 4.78 5.83 4.67 3.83 4.39 4.39 3.33 4.28
5 4.72 4.00 4.11 4.78 0.00 3.44 4.00 4.22 3.67 5.06 5.94 4.72
6 4.50 4.83 4.00 5.83 3.44 0.00 4.11 4.50 4.11 4.50 4.28 4.00
7 3.83 3.33 3.61 4.67 4.00 4.11 0.00 4.83 3.00 4.17 5.94 4.44
8 3.50 3.39 2.94 3.83 4.22 4.50 4.83 0.00 4.17 4.61 6.06 4.28
9 2.39 4.00 5.50 4.39 3.67 4.11 3.00 4.17 0.00 5.72 2.56 5.06
10 3.06 3.39 5.44 4.39 5.06 4.50 4.17 4.61 5.72 0.00 5.00 6.67
11 5.39 2.39 3.17 3.33 5.94 4.28 5.94 6.06 2.56 5.00 0.00 3.56
12 3.17 3.50 5.11 4.28 4.72 4.00 4.44 4.28 5.06 6.67 3.56 0.00
This is probably a frustratingly simple command, but I am truly lost.

Plot multiple tables in a single plot

I have 11 time series which include a temperature column. In other words there are 11 tables which should be presented in a single plot with different lines in color or line type.
t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11
4.14 4.12 4.09 4.07 4.14 4.14 4.12 4.09 4.07 5.70 42.67
4.01 3.99 3.97 3.94 4.01 4.01 3.99 3.97 3.94 4.14 39.98
3.89 3.86 3.84 3.82 3.89 3.89 3.86 3.84 3.82 4.01 38.73
3.77 3.74 3.72 3.69 3.77 3.77 3.74 3.72 3.69 3.89 37.50
3.64 3.62 3.59 3.57 3.64 3.64 3.62 3.59 3.57 3.77 36.25
3.52 3.50 3.48 3.45 3.52 3.52 3.50 3.48 3.45 3.64 35.07
3.40 3.38 3.35 3.33 3.40 3.40 3.38 3.35 3.33 3.52 33.86
3.27 3.24 3.22 3.19 3.27 3.27 3.24 3.22 3.19 3.40 32.52
3.13 3.10 3.07 3.05 3.13 3.13 3.10 3.07 3.05 3.27 31.11
2.99 2.96 2.94 2.91 2.99 2.99 2.96 2.94 2.91 3.13 29.73
2.85 2.81 2.78 2.75 2.85 2.85 2.81 2.78 2.75 2.99 28.23
2.69 2.66 2.63 2.59 2.69 2.69 2.66 2.63 2.59 2.85 26.69
2.53 2.49 2.46 2.42 2.53 2.53 2.49 2.46 2.42 2.69 25.01
2.36 2.33 2.29 2.26 2.36 2.36 2.33 2.29 2.26 2.53 23.36
2.19 2.16 2.13 2.10 2.19 2.19 2.16 2.13 2.10 2.36 21.74
2.05 2.02 1.98 1.95 2.05 2.05 2.02 1.98 1.95 2.19 20.24
One option is :
use melt to reshape data
ggplot2 to plot the melten data
dat$date <- seq(as.Date('2011-01-01'),as.Date('2011-01-31'),
length.out=dim(dat)[1])
library(reshape2)
dat.m <- melt(dat,id='date')
library(ggplot2)
qplot(data=subset(dat.m),x= date, y=value,color=variable, geom='line')
I remove the t11 variable from dataset and replot
qplot(data=subset(dat.m, variable != 't11'),x= date, y=value,
color=variable, geom='line')

Resources