Adding summary columns programmatically - r

I have dataframe X01 whose columns I should summarize with mean, max and min
> head(X01)
B01002e2 B01002e3
1 39.6 47.3
2 37.0 44.8
3 52.6 49.8
4 35.5 26.7
5 39.4 23.9
6 40.8 39.8
My objective is to add min, max, and mean following each column. So far, I have done this manually by rearranging column order, but I will soon have data with many columns which makes this approach very slow:
X01$B01002e2_min <- min(X01$B01002e2, na.rm = TRUE)
X01$B01002e2_max <- max(X01$B01002e2, na.rm = TRUE)
X01$B01002e2_mean <- mean(X01$B01002e2, na.rm = TRUE)
X01$B01002e3_min <- min(X01$B01002e3, na.rm = TRUE)
X01$B01002e3_max <- max(X01$B01002e3, na.rm = TRUE)
X01$B01002e3_mean <- mean(X01$B01002e3, na.rm = TRUE)
X01 <- X01[ , c(1,3,4,5,2,6,7,8)]
> head(X01)
B01002e2 B01002e2_min B01002e2_max B01002e2_mean B01002e3 B01002e3_min B01002e3_max
1 39.6 6 83.7 35.3427547 47.3 8.9 90.8
2 37.0 6 83.7 35.3427547 44.8 8.9 90.8
3 52.6 6 83.7 35.3427547 49.8 8.9 90.8
4 35.5 6 83.7 35.3427547 26.7 8.9 90.8
5 39.4 6 83.7 35.3427547 23.9 8.9 90.8
6 40.8 6 83.7 35.3427547 39.8 8.9 90.8
B01002e3_mean
1 37.6894248
2 37.6894248
3 37.6894248
4 37.6894248
5 37.6894248
6 37.6894248
Is there a solution in R to add these columns after each column being processed in one step, for example with addmargins() ?
dput(head(X01))
structure(list(B01002e2 = c(39.6, 37, 52.6, 35.5, 39.4, 40.8),
B01002e3 = c(47.3, 44.8, 49.8, 26.7, 23.9, 39.8)), .Names = c("B01002e2",
"B01002e3"), row.names = c(NA, 6L), class = "data.frame")

Here's a dplyr approach:
library(dplyr)
X01 %>% mutate_all(funs(max, mean, min))
B01002e2 B01002e3 B01002e2_max B01002e3_max B01002e2_mean B01002e3_mean B01002e2_min B01002e3_min
1 39.6 47.3 52.6 49.8 40.81667 38.71667 35.5 23.9
2 37.0 44.8 52.6 49.8 40.81667 38.71667 35.5 23.9
3 52.6 49.8 52.6 49.8 40.81667 38.71667 35.5 23.9
4 35.5 26.7 52.6 49.8 40.81667 38.71667 35.5 23.9
5 39.4 23.9 52.6 49.8 40.81667 38.71667 35.5 23.9
6 40.8 39.8 52.6 49.8 40.81667 38.71667 35.5 23.9
If you want to ignore NA then you can add na.rm=TRUE:
X01[3,1] = NA
X01 %>% mutate_all(funs(max, mean, min), na.rm=TRUE)
B01002e2 B01002e3 B01002e2_max B01002e3_max B01002e2_mean B01002e3_mean B01002e2_min B01002e3_min
1 39.6 47.3 40.8 49.8 38.46 38.71667 35.5 23.9
2 37.0 44.8 40.8 49.8 38.46 38.71667 35.5 23.9
3 NA 49.8 40.8 49.8 38.46 38.71667 35.5 23.9
4 35.5 26.7 40.8 49.8 38.46 38.71667 35.5 23.9
5 39.4 23.9 40.8 49.8 38.46 38.71667 35.5 23.9
6 40.8 39.8 40.8 49.8 38.46 38.71667 35.5 23.9
If you just want the summary values as a new data frame, you can do this:
X01 %>% summarise_all(funs(max, mean, min), na.rm=TRUE)
B01002e2_max B01002e3_max B01002e2_mean B01002e3_mean B01002e2_min B01002e3_min
1 40.8 49.8 38.46 38.71667 35.5 23.9

Here's an attempt using a functional approach to loop over each column and function:
funs <- c("min","max","mean")
cbind(
dat,
unlist(Map(function(f,d) lapply(d,f), mget(funs, inherits=TRUE), list(dat) ), rec=FALSE)
)
# B01002e2 B01002e3 min.B01002e2 min.B01002e3 max.B01002e2 max.B01002e3 mean.B01002e2 mean.B01002e3
#1 39.6 47.3 35.5 23.9 52.6 49.8 40.81667 38.71667
#2 37.0 44.8 35.5 23.9 52.6 49.8 40.81667 38.71667
#3 52.6 49.8 35.5 23.9 52.6 49.8 40.81667 38.71667
#4 35.5 26.7 35.5 23.9 52.6 49.8 40.81667 38.71667
#5 39.4 23.9 35.5 23.9 52.6 49.8 40.81667 38.71667
#6 40.8 39.8 35.5 23.9 52.6 49.8 40.81667 38.71667

Related

How to plot multiple different state scatterplots using ggplot2? [duplicate]

This question already has answers here:
Plot multiple columns on the same graph in R [duplicate]
(4 answers)
Filter data.frame rows by a logical condition
(9 answers)
Closed 7 months ago.
I am currently analyzing a dataset of yearly C-section rates across the 50 US States between 2004-2020. I want to create 1 scatterplot that contains the rates from Alabama, Mississippi, and Utah. I am having trouble writing the code because I haven't used R in a while. This is what I have so far.
Plot2 <- ggplot(Rates, aes(...1,...2)) +
geom_line() +
ggtitle( "C-Section Rates") +
xlab( "Year") +
ylab( "Percentage of Live Births(%)")
And here is the dataset that I am analyzing
Rate <- read.table(text="YEAR AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY
2020 35 22.9 28.4 33.8 30.5 27.2 34.1 31.7 35.9 33.9 26.3 23.5 30.8 30.1 30.2 30.1 34.3 36.8 29.7 33.7 32.4 32.5 28.5 38.2 29.3 27.6 28.8 32.9 32.1 33.2 26.1 33.6 29.9 27 31.3 32.1 28.8 30.6 33.4 33.5 24.7 32.1 34.7 23.1 26.9 32.6 28.5 34.2 26.7 26.4
2019 34.6 21.6 27.8 34.5 30.8 26.8 34.6 31.5 36.5 34.3 26.8 24 30.6 29.3 29.6 29.7 33.6 36.7 30.2 33 31.4 32 27.6 38.5 30.1 28.4 29.1 32.8 31.6 33.8 26.4 33.2 29.1 26.5 31 32.1 28 30.2 32 33.2 24.5 31.8 34.8 23.1 25.8 31.9 27.8 34.6 26.7 26.3
2018 34.7 22.4 27.5 34.8 30.9 26.1 34.8 31.3 36.8 34 26.9 24 31.2 29.8 29.8 29.7 34.3 37 30.4 33.9 31.5 32.1 27 38.3 30 28.1 29.9 33.8 31.6 34.9 25.3 33.9 29.4 26.5 30.8 32.8 28 30.1 32.2 33.5 24.6 32.4 35 22.7 25.9 32.4 27.9 34.1 26.6 27.4
2017 35.1 22.5 26.9 33.5 31.4 26.5 34.8 31.8 37.2 34.2 25.9 23.7 31.1 29.7 29.7 30 35.2 37.5 29.9 33.9 31.6 31.9 27.4 37.8 30.1 28.5 30.4 34.1 31 35.9 24.7 34.1 29.4 28.3 30.3 32.2 28.1 30.5 31.5 33.5 24.5 32.4 35 22.8 25.7 32.6 27.7 35.2 26.4 26.4
2016 34.4 23 27.5 32.3 31.9 26.2 35.4 31.8 37.4 33.8 25.2 23.9 31.1 29.8 30.1 29.5 34.6 37.5 28.9 33.7 31.3 32 26.8 38.2 30.2 29.1 31 33.8 30.9 36.2 24.8 33.8 29.4 26.8 30.8 32 27.2 29.8 31.2 33.5 25.3 32.5 34.4 22.3 25.7 33 27.4 34.9 26 27.4
2015 35.2 22.9 27.6 32.3 32.3 25.9 34 31.9 37.3 33.6 25.9 24.4 31 29.6 29.8 29.6 34.4 37.5 29.4 34.9 31.4 31.9 26.5 38 30.3 29.7 31.1 34.6 30.8 36.8 24.3 33.8 29.3 27.5 30.4 32.4 27.1 30.1 30.6 33.7 25.7 33.2 34.4 22.8 25.5 32.9 27.5 34.9 26.2 27.3
2014 35.4 23.7 27.8 32 32.7 25.6 34.2 31.5 37.2 33.8 24.6 24.2 31.2 30.3 30 29.8 35.1 38.3 29.8 34.9 31.6 32.8 26.5 37.7 30.1 31.4 30.8 34.4 29.9 37.4 23.8 33.9 29.5 27.6 30.5 33.1 27.4 30.4 30.7 34.3 24.8 33.7 34.9 22.3 25.8 33.1 27.6 35.4 26.1 27.8
2004 31.8 21.9 24.7 31.5 30.7 24.6 32.4 30 34.9 30.5 25.6 22.6 28.8 28.2 26.7 28.9 33.9 36.8 28.3 31.1 32.2 28.8 25.3 35.1 29.7 25.8 28.6 31 28 36.3 22.2 31.5 29.3 26.4 28.1 32.5 27.6 28.9 30.3 32.7 25.1 31.1 32.6 21.6 25.9 31.4 27.8 34.2 23.7 24.6", header=TRUE)
ggplot2 is designed to work most smoothly with "long" aka tidy data, where each row is an observation and each column is a variable. Your original data is "wide," with the states all in separate columns. One way to switch between the two data shapes is pivot_longer from the tidyr package, which is loaded along with ggplot2 when we load tidyverse. You can filter using filter from dplyr, also loaded in tidyverse.
library(tidyverse)
Rate %>%
pivot_longer(-YEAR, names_to = "STATE") %>%
filter(STATE %in% c("AL", "MS", "UT")) %>%
ggplot(aes(YEAR, value, color = STATE)) +
geom_point()

subtract from column with the maximum values in sequential manner in R

I am still learning how to do loops and if-else statements in R. I can do the process in long hand method but I am going to implement them in a large dataset so I need to process them in loops/if-else.
My data looks a little bit like the sample data frame below. One of the columns contain the column number of the maximum value within the row:
x1 x2 x3 x4 x5 x6 x7 max_index max_val
1 56.1 56.8 99.4 44.6 50.4 74.9 17.7 3 99.4
2 9.1 46.1 74.2 64.3 62.3 68.8 85.7 7 85.7
3 83.3 84.5 18.4 93.2 17.6 69.7 23.4 4 93.2
4 94.0 9.7 46.8 25.0 96.9 69.2 94.8 5 96.9
5 21.5 64.1 89.1 87.7 59.7 88.0 73.5 3 89.1
6 53.0 94.9 87.2 19.6 55.9 48.5 82.9 2 94.9
7 52.2 79.1 20.6 9.9 18.3 21.5 92.5 7 92.5
8 42.5 33.0 36.9 45.0 43.9 7.6 45.3 7 45.3
9 89.3 20.6 41.7 74.8 67.4 21.0 49.1 1 89.3
10 21.2 92.6 86.3 76.3 68.6 44.8 8.8 2 92.6
What I want to do is subtract the 3 succeeding columns (from the maximum) from each other like this:
j1 <- max.col(df[,1:7], "first")
df$max_index <- j1
df$max_val <- df[cbind(1:nrow(df), j1)]
i1 <- j1 + 1
i2 <- i1 + 1
i3 <- i2 +1
value <- df[cbind(1:nrow(df), j1)]
value1 <- df[cbind(1:nrow(df), i1)]
value2 <- df[cbind(1:nrow(df), i2)]
value3 <- df[cbind(1:nrow(df), i3)]
df$max_val <- value
df$max.up1 <- value1
df$max.up2 <- value2
df$max.up3 <- value3
df_x1 <- df$max_val - df$max.up1
df_x2 <- df$max.up1 - df$max.up2
df_x3 <- df$max.up2 - df$max.up3
After doing that, I would like to know if all 3 outputs (df_x1, df_x2, df_x3) are all positive and add a column that says "TRUE" and "FALSE" if not.
I would like my final dataframe to look like this:
x1 x2 x3 x4 x5 x6 x7 max_index max_val t.or.f
1 56.1 56.8 99.4 44.6 50.4 74.9 17.7 3 99.4 FALSE
2 9.1 46.1 74.2 64.3 62.3 68.8 85.7 7 85.7 NA
3 83.3 84.5 18.4 93.2 17.6 69.7 23.4 4 93.2 FALSE
4 94.0 9.7 46.8 25.0 96.9 69.2 94.8 5 96.9 NA
5 21.5 64.1 89.1 87.7 59.7 88.0 73.5 3 89.1 FALSE
6 53.0 94.9 87.2 19.6 55.9 48.5 82.9 2 94.9 FALSE
7 52.2 79.1 20.6 9.9 18.3 21.5 92.5 7 92.5 FALSE
8 42.5 33.0 36.9 45.0 43.9 7.6 45.3 7 45.3 FALSE
9 89.3 20.6 41.7 74.8 67.4 21.0 49.1 1 89.3 FALSE
10 21.2 92.6 86.3 76.3 68.6 44.8 8.8 2 92.6 TRUE
How will I simplify my code? Thanks!
I here is data.table solution with structured data approach:
library(data.table)
dt.m <- read.table(text = "
x1 x2 x3 x4 x5 x6 x7 max_index max_val
1 56.1 56.8 99.4 44.6 50.4 74.9 17.7 3 99.4
2 9.1 46.1 74.2 64.3 62.3 68.8 85.7 7 85.7
3 83.3 84.5 18.4 93.2 17.6 69.7 23.4 4 93.2
4 94.0 9.7 46.8 25.0 96.9 69.2 94.8 5 96.9
5 21.5 64.1 89.1 87.7 59.7 88.0 73.5 3 89.1
6 53.0 94.9 87.2 19.6 55.9 48.5 82.9 2 94.9
7 52.2 79.1 20.6 9.9 18.3 21.5 92.5 7 92.5
8 42.5 33.0 36.9 45.0 43.9 7.6 45.3 7 45.3
9 89.3 20.6 41.7 74.8 67.4 21.0 49.1 1 89.3
10 21.2 92.6 86.3 76.3 68.6 44.8 8.8 2 92.6", header = TRUE)
dt.m <- data.table(dt.m)
dt.m[, row.id := 1:.N]
# melt data to make it easy to work with, excluding max.val and max.index
dt <- melt(data = dt.m, measure.vars = 1:7, id.vars = "row.id")
# replicate max.val and max.index which are already provided in example
dt[, max.val := max(value), by = row.id]
dt[, max.index := which(value == max.val), by = row.id]
dt[, x.index := 1:.N, by = row.id]
# filter to values after the max value
out <- dt[x.index >= max.index]
# keep max value and 3 values post max value
out <- out[, post.max.index := 1:.N, by = row.id][post.max.index <= 4]
out <- out[order(row.id, x.index)]
out[, previous.x := shift(value)]
out[, change.x := previous.x - value]
out <- out[max.index != x.index]
# check if all values are positive
res <- out[, .(all.next.positive = all(change.x > 0)), by = row.id]
# add result to the original data
dt.m <- merge(dt.m, res, by = "row.id", all.x = TRUE)

SVG use one color for each path

I have an svg and in in there are 2 paths
<path d="M 84.4 53.2 C 75.3 50.1 67.1 48.5 59.6 48.3 C 51.2 48 45.2 47.5 41.5 46.5 C 35.7 45.1 29.9 42.4 23.9 38.4 C 21.6 36.9 19.4 35.2 17.3 33.2 C 19.4 34.2 21.6 35 23.9 35.6 C 27.3 36.5 34.1 37 44.1 37.2 C 53.7 37.3 59.7 37.7 62 38.3 C 70.4 40.3 77 44.2 81.8 49.9 C 82.9 51 83.7 52 84.4 53.2 Z" class="st0" style="fill: rgb(15, 141, 225);"/>
<path d="M 68.1 7.3 C 79.5 10.1 86.8 16 89.9 24.9 C 91.4 29.2 91.4 36.1 89.8 45.7 C 89.9 45.8 90 46 90.1 46.2 L 90 46 C 90.4 46.7 90.8 47.4 91.2 48.2 C 93.2 52 94.4 56.2 95 60.7 C 95.1 61.3 95 61.8 94.6 62.3 C 94.2 62.7 93.8 63 93.2 63.1 C 92.7 63.2 92.1 63 91.6 62.7 C 91.2 62.3 90.9 61.9 90.8 61.3 C 90.3 57.3 89.2 53.6 87.5 50.2 C 86.8 49.1 86 48.1 85.1 47.1 C 79.7 40.8 72.4 36.5 63.1 34.2 L 63.2 34.3 C 60.8 33.607 54.5 33.599 44.3 33.1 C 34.709 32.63 28.3 32.4 25.1 31.6 L 25 31.6 C 18.9 30.1 14.1 26.9 10.6 22 C 10.2 21.4 9.8 20.7 9.5 20.1 C 9.5 20.1 9.5 20 9.4 20 C 9.3 19.8 9.2 19.6 9 19.3 C 6.6 15.3 5.3 10.8 5 6.1 C 7.4 8.5 10.3 10.1 13.5 10.9 C 17.7 11.9 22.3 12.1 27.3 11.5 C 30.2 11.2 34.7 10.4 40.6 9 C 46.4 7.5 50.9 6.6 54 6.4 C 59 5.8 63.7 6.1 68.1 7.3 Z" class="st0" style="fill: rgb(11, 59, 91);"/>
Is is possible to select and fill the paths individually with css?
This works
.svg-fill {
fill: red
}
but it fills the whole svg. How can I target the individual paths?
Use nth-child selector:
svg path:nth-child(1) { /*OR svg path:first-child OR svg path:nth-of-type(1)*/
fill:red;
}
svg path:nth-child(2) { /*OR svg path:last-child OR svg path:nth-of-type(2)*/
fill:green;
}
<svg>
<path d="M 84.4 53.2 C 75.3 50.1 67.1 48.5 59.6 48.3 C 51.2 48 45.2 47.5 41.5 46.5 C 35.7 45.1 29.9 42.4 23.9 38.4 C 21.6 36.9 19.4 35.2 17.3 33.2 C 19.4 34.2 21.6 35 23.9 35.6 C 27.3 36.5 34.1 37 44.1 37.2 C 53.7 37.3 59.7 37.7 62 38.3 C 70.4 40.3 77 44.2 81.8 49.9 C 82.9 51 83.7 52 84.4 53.2 Z"/>
<path d="M 68.1 7.3 C 79.5 10.1 86.8 16 89.9 24.9 C 91.4 29.2 91.4 36.1 89.8 45.7 C 89.9 45.8 90 46 90.1 46.2 L 90 46 C 90.4 46.7 90.8 47.4 91.2 48.2 C 93.2 52 94.4 56.2 95 60.7 C 95.1 61.3 95 61.8 94.6 62.3 C 94.2 62.7 93.8 63 93.2 63.1 C 92.7 63.2 92.1 63 91.6 62.7 C 91.2 62.3 90.9 61.9 90.8 61.3 C 90.3 57.3 89.2 53.6 87.5 50.2 C 86.8 49.1 86 48.1 85.1 47.1 C 79.7 40.8 72.4 36.5 63.1 34.2 L 63.2 34.3 C 60.8 33.607 54.5 33.599 44.3 33.1 C 34.709 32.63 28.3 32.4 25.1 31.6 L 25 31.6 C 18.9 30.1 14.1 26.9 10.6 22 C 10.2 21.4 9.8 20.7 9.5 20.1 C 9.5 20.1 9.5 20 9.4 20 C 9.3 19.8 9.2 19.6 9 19.3 C 6.6 15.3 5.3 10.8 5 6.1 C 7.4 8.5 10.3 10.1 13.5 10.9 C 17.7 11.9 22.3 12.1 27.3 11.5 C 30.2 11.2 34.7 10.4 40.6 9 C 46.4 7.5 50.9 6.6 54 6.4 C 59 5.8 63.7 6.1 68.1 7.3 Z" />
</svg>
Each of your paths has the same class (at the end of each path you can see it is defined with class="st0" in the code). If you change the class to make each one unique you can target them individually.
For example, change the second one to class="st1" and then the following CSS will make the 1st path red and the 2nd blue:
.st0 { fill: red; }
.st1 { fill: blue; }
If you want to change them using CSS you should also remove the style="fill: rgb(xx, xx, xx);" from each path.

Turn a data frame into a ts in R

I want to turn a list data structure into a double data structure:
Starting list data from my CSV.
> my_data = read.csv("time series test.csv", header = TRUE)
> my_data
Date Value
1 1/1/2014 123.0000
2 1/2/2014 128.5693
3 1/3/2014 129.1474
4 1/4/2014 130.1361
5 1/5/2014 137.9758
6 1/6/2014 141.0548
7 1/7/2014 141.3517
8 1/8/2014 142.1449
9 1/9/2014 142.1479
10 1/10/2014 149.8912
Example double:
> nottem
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1920 40.6 40.8 44.4 46.7 54.1 58.5 57.7 56.4 54.3 50.5 42.9 39.8
1921 44.2 39.8 45.1 47.0 54.1 58.7 66.3 59.9 57.0 54.2 39.7 42.8
1922 37.5 38.7 39.5 42.1 55.7 57.8 56.8 54.3 54.3 47.1 41.8 41.7
1923 41.8 40.1 42.9 45.8 49.2 52.7 64.2 59.6 54.4 49.2 36.3 37.6
1924 39.3 37.5 38.3 45.5 53.2 57.7 60.8 58.2 56.4 49.8 44.4 43.6
1925 40.0 40.5 40.8 45.1 53.8 59.4 63.5 61.0 53.0 50.0 38.1 36.3
1926 39.2 43.4 43.4 48.9 50.6 56.8 62.5 62.0 57.5 46.7 41.6 39.8
1927 39.4 38.5 45.3 47.1 51.7 55.0 60.4 60.5 54.7 50.3 42.3 35.2
1928 40.8 41.1 42.8 47.3 50.9 56.4 62.2 60.5 55.4 50.2 43.0 37.3
1929 34.8 31.3 41.0 43.9 53.1 56.9 62.5 60.3 59.8 49.2 42.9 41.9
1930 41.6 37.1 41.2 46.9 51.2 60.4 60.1 61.6 57.0 50.9 43.0 38.8
1931 37.1 38.4 38.4 46.5 53.5 58.4 60.6 58.2 53.8 46.6 45.5 40.6
1932 42.4 38.4 40.3 44.6 50.9 57.0 62.1 63.5 56.3 47.3 43.6 41.8
1933 36.2 39.3 44.5 48.7 54.2 60.8 65.5 64.9 60.1 50.2 42.1 35.8
1934 39.4 38.2 40.4 46.9 53.4 59.6 66.5 60.4 59.2 51.2 42.8 45.8
1935 40.0 42.6 43.5 47.1 50.0 60.5 64.6 64.0 56.8 48.6 44.2 36.4
1936 37.3 35.0 44.0 43.9 52.7 58.6 60.0 61.1 58.1 49.6 41.6 41.3
1937 40.8 41.0 38.4 47.4 54.1 58.6 61.4 61.8 56.3 50.9 41.4 37.1
1938 42.1 41.2 47.3 46.6 52.4 59.0 59.6 60.4 57.0 50.7 47.8 39.2
1939 39.4 40.9 42.4 47.8 52.4 58.0 60.7 61.8 58.2 46.7 46.6 37.8
So how do I turn my_data into the same structure as nottem, so I can use it with STL()? plot(stl(nottem, "per"))
You can use the ts function. c(2014,1) is January 2014.
myts <- ts(my_data[,2], start = c(2014,1), frequency = 12)

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