R output giving wrong values for difference between columns - r

I have this tibble called data1:
structure(list(subject = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12), treatment = c("099526-01", "099526-01", "099526-01", "099526-01",
"099526-01", "099526-01", "099526-01", "099526-01", "099526-01",
"099526-01", "099526-01", "099526-01"), T0 = c(34.35, 26.5, 29.65,
11.575, 34.4, 25.775, 33, 31.6, 18.35, 36.275, 36.075, 34.225
), T15min = c(34.85, 28.95, 30.2, 11.05, 34.1, 22.025, 25.325,
31.775, 17.8, 31.7, 35.35, 34.25), T2h = c(33.425, 26.125, 27.65,
11.475, 36.95, 22.975, 30.025, 31.775, 18.025, 33.025, 34.125,
34.55), T4h = c(35.7, 26.075, 29.3, 13.275, 36.45, 28.475, 30.925,
32.15, 17.425, 34.95, 34.55, 34.775), T6h = c(36.225, 28.15,
29.1, 12.25, 34.275, 26.05, 28.1, 34.025, 17.775, 35.3, 35.125,
36.725), T8h = c(34.9, 25.75, 30.425, 10.75, 34.425, 28.725,
28.475, 34.35, 19.325, 33.925, 36.95, 38.225)), row.names = c(NA,
-12L), class = c("tbl_df", "tbl", "data.frame"))
subject treatment T0 T15min T2h T4h T6h T8h
<dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 099526-01 34.4 34.8 33.4 35.7 36.2 34.9
2 2 099526-01 26.5 29.0 26.1 26.1 28.2 25.8
3 3 099526-01 29.6 30.2 27.6 29.3 29.1 30.4
4 4 099526-01 11.6 11.0 11.5 13.3 12.2 10.8
5 5 099526-01 34.4 34.1 37.0 36.4 34.3 34.4
6 6 099526-01 25.8 22.0 23.0 28.5 26.0 28.7
7 7 099526-01 33 25.3 30.0 30.9 28.1 28.5
8 8 099526-01 31.6 31.8 31.8 32.2 34.0 34.4
9 9 099526-01 18.4 17.8 18.0 17.4 17.8 19.3
10 10 099526-01 36.3 31.7 33.0 35.0 35.3 33.9
11 11 099526-01 36.1 35.4 34.1 34.6 35.1 37.0
12 12 099526-01 34.2 34.2 34.6 34.8 36.7 38.2
I'm creating a new tibble with new columns as the difference of times to T0 (e.g., T15min-T0, T2h-T0), as follows:
data2 <- data1 %>%
mutate(delta_1 = .[[4]] - .[[3]],
delta_2 = .[[5]] - .[[3]],
delta_3 = .[[6]] - .[[3]],
delta_4 = .[[7]] - .[[3]],
delta_5 = .[[8]] - .[[3]])
subject treatment T0 T15min T2h T4h T6h T8h delta_1 delta_2 delta_3 delta_4 delta_5
<dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 099526-01 34.4 34.8 33.4 35.7 36.2 34.9 0.5 -0.925 1.35 1.88 0.550
2 2 099526-01 26.5 29.0 26.1 26.1 28.2 25.8 2.45 -0.375 -0.425 1.65 -0.75
3 3 099526-01 29.6 30.2 27.6 29.3 29.1 30.4 0.550 -2 -0.350 -0.550 0.775
4 4 099526-01 11.6 11.0 11.5 13.3 12.2 10.8 -0.525 -0.100 1.70 0.675 -0.825
5 5 099526-01 34.4 34.1 37.0 36.4 34.3 34.4 -0.300 2.55 2.05 -0.125 0.0250
6 6 099526-01 25.8 22.0 23.0 28.5 26.0 28.7 -3.75 -2.80 2.70 0.275 2.95
7 7 099526-01 33 25.3 30.0 30.9 28.1 28.5 -7.68 -2.98 -2.07 -4.9 -4.52
8 8 099526-01 31.6 31.8 31.8 32.2 34.0 34.4 0.175 0.175 0.550 2.43 2.75
9 9 099526-01 18.4 17.8 18.0 17.4 17.8 19.3 -0.550 -0.325 -0.925 -0.575 0.975
10 10 099526-01 36.3 31.7 33.0 35.0 35.3 33.9 -4.57 -3.25 -1.32 -0.975 -2.35
11 11 099526-01 36.1 35.4 34.1 34.6 35.1 37.0 -0.725 -1.95 -1.53 -0.950 0.875
12 12 099526-01 34.2 34.2 34.6 34.8 36.7 38.2 0.0250 0.325 0.550 2.50 4
However, the differences are not correct. For example, for the first subject, T2h - T0 (33.4 - 34.4) should result -1, and not -0.925
What could be wrong with the code?

The code is correct. However, it appears that the df output (View) is limited to 1 decimal, while your values have 3 decimals.
Try running
options(digits = 5)
at the top of your script.

Related

dplyr - programming dynamic variable & function name - ascending & descending

I am trying to find way to shorten my code using dynamic naming variables & functions related with ascending & descending order. Though I can do desc but couldn't find anything for ascending. Below is the reproducible example to demonstrate my problem.
Here is the sample dataset
library(dplyr)
set.seed(100)
data <- tibble(a = runif(20, min = 0, max = 100),
b = runif(20, min = 0, max = 100),
c = runif(20, min = 0, max = 100))
Dynamically passing variable with percent rank in ascending order
current_var <- "a" # dynamic variable name
data %>%
mutate("percent_rank_{current_var}" := percent_rank(!!sym(current_var)))
#> # A tibble: 20 × 4
#> a b c percent_rank_a
#> <dbl> <dbl> <dbl> <dbl>
#> 1 30.8 53.6 33.1 0.263
#> 2 25.8 71.1 86.5 0.158
#> 3 55.2 53.8 77.8 0.684
#> 4 5.64 74.9 82.7 0
#> 5 46.9 42.0 60.3 0.526
#> 6 48.4 17.1 49.1 0.579
#> 7 81.2 77.0 78.0 0.947
#> 8 37.0 88.2 88.4 0.421
#> 9 54.7 54.9 20.8 0.632
#> 10 17.0 27.8 30.7 0.0526
#> 11 62.5 48.8 33.1 0.737
#> 12 88.2 92.9 19.9 1
#> 13 28.0 34.9 23.6 0.211
#> 14 39.8 95.4 27.5 0.474
#> 15 76.3 69.5 59.1 0.895
#> 16 66.9 88.9 25.3 0.789
#> 17 20.5 18.0 12.3 0.105
#> 18 35.8 62.9 23.0 0.316
#> 19 35.9 99.0 59.8 0.368
#> 20 69.0 13.0 21.1 0.842
Dynamically passing variable with percent rank in descending order
data %>%
mutate("percent_rank_{current_var}" := percent_rank(desc(!!sym(current_var))))
#> # A tibble: 20 × 4
#> a b c percent_rank_a
#> <dbl> <dbl> <dbl> <dbl>
#> 1 30.8 53.6 33.1 0.737
#> 2 25.8 71.1 86.5 0.842
#> 3 55.2 53.8 77.8 0.316
#> 4 5.64 74.9 82.7 1
#> 5 46.9 42.0 60.3 0.474
#> 6 48.4 17.1 49.1 0.421
#> 7 81.2 77.0 78.0 0.0526
#> 8 37.0 88.2 88.4 0.579
#> 9 54.7 54.9 20.8 0.368
#> 10 17.0 27.8 30.7 0.947
#> 11 62.5 48.8 33.1 0.263
#> 12 88.2 92.9 19.9 0
#> 13 28.0 34.9 23.6 0.789
#> 14 39.8 95.4 27.5 0.526
#> 15 76.3 69.5 59.1 0.105
#> 16 66.9 88.9 25.3 0.211
#> 17 20.5 18.0 12.3 0.895
#> 18 35.8 62.9 23.0 0.684
#> 19 35.9 99.0 59.8 0.632
#> 20 69.0 13.0 21.1 0.158
How to combine both into one statement? - I can do for desc but couldn't find any explicit statement for ascending order
rank_function <- desc # dynamic function for ranking
data %>%
mutate("percent_rank_{current_var}" := percent_rank(rank_function(!!sym(current_var))))
#> # A tibble: 20 × 4
#> a b c percent_rank_a
#> <dbl> <dbl> <dbl> <dbl>
#> 1 30.8 53.6 33.1 0.737
#> 2 25.8 71.1 86.5 0.842
#> 3 55.2 53.8 77.8 0.316
#> 4 5.64 74.9 82.7 1
#> 5 46.9 42.0 60.3 0.474
#> 6 48.4 17.1 49.1 0.421
#> 7 81.2 77.0 78.0 0.0526
#> 8 37.0 88.2 88.4 0.579
#> 9 54.7 54.9 20.8 0.368
#> 10 17.0 27.8 30.7 0.947
#> 11 62.5 48.8 33.1 0.263
#> 12 88.2 92.9 19.9 0
#> 13 28.0 34.9 23.6 0.789
#> 14 39.8 95.4 27.5 0.526
#> 15 76.3 69.5 59.1 0.105
#> 16 66.9 88.9 25.3 0.211
#> 17 20.5 18.0 12.3 0.895
#> 18 35.8 62.9 23.0 0.684
#> 19 35.9 99.0 59.8 0.632
#> 20 69.0 13.0 21.1 0.158
Created on 2022-08-17 by the reprex package (v2.0.1)
You could compose a function to return its input:
rank_function <- function(x) x
Actually this function has been defined in base, i.e. identity.
rank_function <- identity
Also, you can explore the source code of desc:
desc
function (x) -xtfrm(x)
Apparently desc is just the opposite number of xtfrm. So you can use it for ascending ordering.
rank_function <- xtfrm
In the help document of xtfrm(x):
A generic auxiliary function that produces a numeric vector which will sort in the same order as x.

Tidyverse: Error in as.matrix : attempt to apply non-function

I am trying to calculate SPEI values using SPEI package and Hargreaves method. I want to automate the process so that I can calculate SPEI for all 6 stations in one go and save them to a new file spei.3.
SPEI is calculated in three steps. First, we calculate PET values (spei_pet), which is then subtracted from Precipitation value to calculate climatic water balance (spei_cwbal). The CWBAL value is then used in SPEI function from the package of the same name with a scale to calculate SPEI values.
I am new to R and very new to tidyverse, but the internet says they are easier to work on. I wrote the code below to do my task. But I am surely missing something (or maybe, many things) because the code throws an error. Please help me identify error in my code, and help me get a solution.
library(tidyverse)
library(SPEI)
file_path = "I:/Proj/Excel sheets - climate/SPI/heatmap/spei_forecast_data.xlsx"
file_forecast = openxlsx::read.xlsx(file_path)
##spei calculation
spei.scale = c(3, 6, 9, 12, 15, 24)
stations = c(1:3, 5:7)
lat = c(23.29, 23.08, 22.95, 22.62, 22.43, 22.40)
lat.fn = function(i) {
if (i <= 3)
lat.fn = lat[i]
else if (i == 5)
lat.fn = lat[4]
else if (i == 6)
lat.fn = lat[5]
else if (i == 7)
lat.fn = lat[6]
}
for ( i in stations) {
file_forecast %>%
mutate(spei_pet[i] <- hargreaves(Tmin = file_forecast$paste("tmin", i),
Tmax = file_forecast$paste("tmax", i),
Pre = file_forecast$paste("p", i),
lat = lat.fn[i])) %>%
mutate(spei_cwbal[i] <- spei_pet[[i]] - file_forecast$paste("p", i)) %>%
mutate(spei.3[i] <- spei(spei_cwbal[[i]], scale = 3))
}
It throws an error
Error in as.matrix(Tmin) : attempt to apply non-function
lat.fn[i] also throws an error, which gets rectified if I use no i. But I need to use some kind of function so that lat.fn takes different value depending on i.
Error in lat.fn[i] : object of type 'closure' is not subsettable
Thanks.
Edit: The data is in the form of a data.frame. I converted it into a tibble to give an idea of what it looks like.
> file_forecast
# A tibble: 960 x 20
Month p7 p6 p5 p3 p2 p1 tmax7 tmax6 tmax5 tmax3 tmax2 tmax1 tmin7 tmin6 tmin5 tmin3 tmin2 tmin1
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Jan 0.162 0.185 0.293 0.436 0.529 0.658 26.4 26.5 26.2 25.9 25.7 24.9 9.57 9.75 10.0 10.4 9.94 9.77
2 Feb 0.207 0.305 0.250 0.260 0.240 0.186 32.2 32.2 32.1 31.9 31.8 30.9 12.4 12.7 12.7 13.0 12.2 11.9
3 Mar 0.511 0.650 0.602 0.636 0.625 0.501 37.3 37.1 37.1 37.0 36.9 36.1 18.7 19.3 18.3 18.0 17.3 16.9
4 Apr 0.976 1.12 1.05 1.12 1.17 1.16 39.5 39.2 39.6 39.5 39.5 38.8 22.8 23.2 22.5 22.2 21.7 20.8
5 May 3.86 4.12 3.76 4.29 4.15 3.84 38.2 37.9 38.3 38.1 38.2 37.6 25.1 25.4 24.9 24.7 24.5 23.8
6 Jun 7.31 8.27 7.20 8.51 9.14 8.76 38.0 37.6 38.1 38.0 38.0 37.7 27.2 27.3 26.9 26.7 26.6 26.1
7 Jul 13.9 15.6 13.2 17.0 19.1 17.8 33.9 33.6 34.0 33.9 33.8 33.5 26.8 26.9 26.6 26.5 26.4 26.0
8 Aug 15.2 17.2 14.4 18.6 20.1 18.4 32.6 32.4 32.7 32.4 32.3 32.0 26.2 26.4 26.1 25.9 25.9 25.4
9 Sep 11.4 11.9 10.5 12.9 13.2 13.1 31.9 31.9 31.8 31.5 31.5 30.9 24.4 24.6 24.3 24.3 24.3 23.7
10 Oct 5.19 5.76 4.81 5.40 5.44 5.04 29.8 30.0 29.6 29.3 29.3 28.6 20.9 21.1 20.8 20.9 20.8 20.2
# ... with 950 more rows, and 1 more variable: year <dbl>

Removed rows containing missing values ggmap

So I'm trying to make a 2d latitude-longitude map with the package ggmap and I'm encountering a problem:
dataset:
slddataset
# A tibble: 382 x 17
station year jd sl_pa sst sss ssf depth sbt sbs sbf gravel sand silt clay lat long
<int> <int> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 4101 2014 142 0 11.7 25.0 0.419 39.9 4.95 31.9 0.320 2.36 97.5 0.110 0.0300 42.2 70.3
2 4102 2014 142 0 11.3 37.8 0.509 27.6 5.03 31.9 0.372 0.390 99.5 0.0700 0.0200 42.2 70.3
3 4104 2014 142 0 11.3 41.2 0.803 24.9 5.50 31.7 0.556 0.700 99.2 0.0800 0.0700 42.2 70.3
4 4105 2014 142 0 10.6 30.8 0.808 28.3 5.14 31.9 0.596 6.83 93.1 0.0700 0.0300 42.2 70.2
5 4106 2014 142 0 10.5 30.7 0.693 35.6 4.93 32.1 0.887 10.8 89.1 0.0500 0.0700 42.2 70.2
6 4107 2014 142 0 11.0 30.7 0.724 41.3 4.44 32.3 0.684 11.3 88.5 0.110 0.120 42.2 70.2
7 4108 2014 142 0 10.3 30.8 0.741 44.4 4.28 32.5 0.340 4.77 95.0 0.110 0.100 42.2 70.1
8 4109 2014 142 0 9.97 30.9 0.980 44.3 4.32 32.4 0.398 7.80 92.0 0.110 0.110 42.2 70.1
9 4110 2014 142 0 10.9 30.7 0.794 41.2 4.60 32.3 0.592 10.3 89.5 0.100 0.0900 42.2 70.2
10 4113 2014 143 0 12.0 30.5 0.684 32.2 4.98 31.9 0.336 0.320 99.6 0.0600 0.0300 42.2 70.3
# ... with 372 more rows
error:
library(ggmap)
stellwagen<-ggmap(get_googlemap(center="stellwagen bank",zoom=7,maptype = "satellite"))
stellwagen + geom_point(aes(x=long, y=lat, color=sl_pa),data=slddataset)
Warning message: Removed 382 rows containing missing values
(geom_point).
Anyone have any ideas?
I think your longitudes are wrong in slddataset. They should all be negative. After correcting those, I can plot the points on the map.
library(dplyr)
library(ggmap)
slddataset <- slddataset %>% mutate(long = long * -1)
stellwagen<-ggmap(get_googlemap(center="stellwagen bank",zoom=7,maptype = "satellite"))
stellwagen +
geom_point(aes(x=long, y=lat),data=slddataset)
DATA
slddataset <- read.table(text = "station year jd sl_pa sst sss ssf depth sbt sbs sbf gravel sand silt clay lat long
1 4101 2014 142 0 11.7 25.0 0.419 39.9 4.95 31.9 0.320 2.36 97.5 0.110 0.0300 42.2 70.3
2 4102 2014 142 0 11.3 37.8 0.509 27.6 5.03 31.9 0.372 0.390 99.5 0.0700 0.0200 42.2 70.3
3 4104 2014 142 0 11.3 41.2 0.803 24.9 5.50 31.7 0.556 0.700 99.2 0.0800 0.0700 42.2 70.3
4 4105 2014 142 0 10.6 30.8 0.808 28.3 5.14 31.9 0.596 6.83 93.1 0.0700 0.0300 42.2 70.2
5 4106 2014 142 0 10.5 30.7 0.693 35.6 4.93 32.1 0.887 10.8 89.1 0.0500 0.0700 42.2 70.2
6 4107 2014 142 0 11.0 30.7 0.724 41.3 4.44 32.3 0.684 11.3 88.5 0.110 0.120 42.2 70.2
7 4108 2014 142 0 10.3 30.8 0.741 44.4 4.28 32.5 0.340 4.77 95.0 0.110 0.100 42.2 70.1
8 4109 2014 142 0 9.97 30.9 0.980 44.3 4.32 32.4 0.398 7.80 92.0 0.110 0.110 42.2 70.1
9 4110 2014 142 0 10.9 30.7 0.794 41.2 4.60 32.3 0.592 10.3 89.5 0.100 0.0900 42.2 70.2
10 4113 2014 143 0 12.0 30.5 0.684 32.2 4.98 31.9 0.336 0.320 99.6 0.0600 0.0300 42.2 70.3",
header = TRUE, stringsAsFactors = FALSE)

Shift time series

I have 2 weekly time-series, which show a small correlation (~0.33).
How can i 'shift in time' one of these series, so that i can check if there's a
greater correlation in the data?
Example data:
x = textConnection('1530.2 1980.9 1811 1617 1585.4 1951.8 2146.6 1605 1395.2 1742.6 2206.5 1839.4 1699.1 1665.9 2144.7 2189.1 1718.4 1615.5 2003.3 2267.6 1772.1 1635.2 1836 2261.8 1799.1 1634.9 1638.6 2056.5 2201.4 1726.8 1586.4 1747.9 1982 1695.2 1624.9 1652.4 2011.9 1788.8 1568.4 1540.7 1866.1 2097.3 1601.3 1458.6 1424.4 1786.9 1628.4 1467.4 1476.2 1823 1736.7 1482.7 1334.2 1871.9 1752.9 1471.6 1583.2 1601.4 1987.7 1649.6 1530.9 1547.1 2165.2 1852 1656.9 1605.2 2184.6 1972 1617.6 1491.1 1709.5 2042.2 1667.1 1542.6 1497.6 2090.5 1816.8 1487.5 1468.2 2228.5 1889.9 1690.8 1395.7 1532.8 1934.4 1557.1 1570.6 1453.2 1669.6 1782 1526.1 1411 1608.1 1740.5 1492.3 1477.8 1102.6 1366.1 1701.1 1500.6 1403.2 1787.2 1776.6 1465.3 1429.5')
x = scan(x)
y = textConnection('29.8 22.6 26 24.8 28.9 27.3 26 29.2 28.2 23.9 24.5 23.6 21.1 22 20.7 19.9 22.8 25 21.6 19.1 27.2 23.7 24.2 22.4 25.5 25.4 23.4 24.7 27.4 23.4 25.8 28.8 27.7 23.7 22.9 29.4 22.6 28.6 22.2 27.6 26.2 26.2 29.8 31.5 24.5 28.7 25.9 26.9 25.9 30.5 30.5 29.4 29.3 31.4 30 27.9 28.5 26.4 29.5 28.4 25.1 24.6 21.1 23.6 20.5 23.7 25.3 20.2 23.4 21.1 23.1 24.6 20.7 20.7 26.9 24.1 24.7 25.8 26.7 26 28.9 29.5 27.4 22.1 31.6 25 27.4 30.4 28.9 27.4 22.5 28.4 28.7 31.1 29.3 28.3 30.6 28.6 26 26.2 26.2 26.7 25.6 31.5 30.9')
y = scan(y)
I'm using R with dtw package, but i'm not familiar with these kind of algorithms.
Thanks for any help!
You could try the ccf() function in base R. This estimates the cross-correlation function of the two time series.
For example, using your data (see below if interested in how I got the data you pasted into your Question into R objects x and y)
xyccf <- ccf(x, y)
yielding
> xyccf
Autocorrelations of series ‘X’, by lag
-17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7
0.106 0.092 0.014 0.018 0.011 0.029 -0.141 -0.153 -0.107 -0.141 -0.221
-6 -5 -4 -3 -2 -1 0 1 2 3 4
-0.274 -0.175 -0.277 -0.176 -0.217 -0.253 -0.339 -0.274 -0.267 -0.330 -0.278
5 6 7 8 9 10 11 12 13 14 15
-0.184 -0.120 -0.200 -0.156 -0.184 -0.062 -0.076 -0.117 -0.048 0.015 -0.016
16 17
-0.038 -0.029
and this plot
To interpret this, when the lag is positive, y is leading x whereas when the lag is negative x is leading y.
Reading your data into R...
x <- scan(text = "1530.2 1980.9 1811 1617 1585.4 1951.8 2146.6 1605 1395.2 1742.6
2206.5 1839.4 1699.1 1665.9 2144.7 2189.1 1718.4 1615.5 2003.3
2267.6 1772.1 1635.2 1836 2261.8 1799.1 1634.9 1638.6 2056.5
2201.4 1726.8 1586.4 1747.9 1982 1695.2 1624.9 1652.4 2011.9
1788.8 1568.4 1540.7 1866.1 2097.3 1601.3 1458.6 1424.4 1786.9
1628.4 1467.4 1476.2 1823 1736.7 1482.7 1334.2 1871.9 1752.9
1471.6 1583.2 1601.4 1987.7 1649.6 1530.9 1547.1 2165.2 1852
1656.9 1605.2 2184.6 1972 1617.6 1491.1 1709.5 2042.2 1667.1
1542.6 1497.6 2090.5 1816.8 1487.5 1468.2 2228.5 1889.9 1690.8
1395.7 1532.8 1934.4 1557.1 1570.6 1453.2 1669.6 1782 1526.1
1411 1608.1 1740.5 1492.3 1477.8 1102.6 1366.1 1701.1 1500.6
1403.2 1787.2 1776.6 1465.3 1429.5")
y <- scan(text = "29.8 22.6 26 24.8 28.9 27.3 26 29.2 28.2 23.9 24.5 23.6 21.1 22
20.7 19.9 22.8 25 21.6 19.1 27.2 23.7 24.2 22.4 25.5 25.4 23.4
24.7 27.4 23.4 25.8 28.8 27.7 23.7 22.9 29.4 22.6 28.6 22.2 27.6
26.2 26.2 29.8 31.5 24.5 28.7 25.9 26.9 25.9 30.5 30.5 29.4 29.3
31.4 30 27.9 28.5 26.4 29.5 28.4 25.1 24.6 21.1 23.6 20.5 23.7
25.3 20.2 23.4 21.1 23.1 24.6 20.7 20.7 26.9 24.1 24.7 25.8 26.7
26 28.9 29.5 27.4 22.1 31.6 25 27.4 30.4 28.9 27.4 22.5 28.4 28.7
31.1 29.3 28.3 30.6 28.6 26 26.2 26.2 26.7 25.6 31.5 30.9")

Inserting another column to a data frame and incrementing its value per row

I have this data frame:
head(df,10)
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
3 36.4 13.1 13.9 36.6 9.26 57.9 28.0 34.96 26049 3492
4 31.1 11.2 12.6 45.1 7.81 48.8 25.9 37.85 17515 2754
5 33.2 13.4 13.2 40.3 8.69 54.3 26.9 35.67 23510 3265
6 34.0 12.8 13.7 39.4 8.77 54.8 26.5 35.19 25151 3305
7 32.7 12.4 13.6 41.3 8.49 53.0 25.9 35.97 25214 3201
8 33.4 13.7 12.5 40.3 8.76 54.7 27.1 36.50 23943 3391
9 35.2 13.8 13.5 37.5 9.20 57.5 27.8 33.08 25647 3385
10 34.6 14.9 14.9 35.6 9.35 58.4 27.8 35.81 27324 3790
11 30.4 13.3 13.0 43.3 8.29 51.8 24.9 38.31 25178 2881
12 32.0 13.3 14.0 40.7 8.58 53.6 26.1 35.97 25677 3162
I have DateTime is this:
DateTime<-Sys.time()
I would like to insert another column this df and increment the DateTime value by 30 seconds for each row.
Im doing this:
for (i in 1:nrow(df)) {
df[1,]$DateTime<-DateTime
DateTime<-DateTime+30
}
This loop is not doing what Im trying to do. Any help is greatly appreicated.
df$DateTime <- Sys.time() + 30 * (seq_len(nrow(df))-1)

Resources