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)
Related
I need to run a self-made function across rows and create an output column in the same data frame (column name tt_daily). This is some made up example.
#data
data1 <- read.csv(text = "
doy,tmx,tmn,relHum,srad
148,31.3,13.8,68.3,30.4
149,31.1,17.2,62.2,30
150,30.1,16.1,69.7,20.9
151,27.3,16.2,77.1,26.1
152,33.4,18.4,65.9,27.4
153,27.2,18,70.3,26.6
154,30.3,13,71.5,28.4
155,36.2,22,62.2,28.8
156,32.9,22.2,61.1,24.9
157,30.5,16.2,63.2,27.9
158,25.7,19.3,71,18.3
159,29.1,18.3,87.2,12.7
160,28.5,20.3,70.2,24.8
")
This is the function:
# function to run row wise
tb<- 11
topt<- 30
tmax<- 42
tt<-function(tmx, tmn, tb, topt, tmax){
tmean<- (tmx + tmn) / 2
if(tmean <= tb) {t1 = 0}
if(tmean >tb & tmean <=topt) {t1 = tmean - tb}
if(tmean>topt & tmean<max) {t1 = (topt - tb) / (topt - tmax) * (tmean - tmax)}
if(tmean >= tmax) {t1 <- 0}
return(t1)
}
This is two options of what I did:
#Option 1
library(dplyr)
tt.example <- data1 %>%
mutate(tt_daily = purrr::pmap(function(tmx, tmn, tb, topt, tmax) tt))
and this is the error:
Error: Problem with mutate() column tt_daily.
i tt_daily = purrr::pmap(function(tmx, tmn, tb, topt, tmax) tt).
x argument ".f" is missing, with no default
This is the option 2:
#Option 2
tt.example <- data1 %>%
rowwise() %>%
mutate(tt_daily = tt(tmx, tmn, tb, topt, tmax))
This is the error I got:
Error: Problem with mutate() column tt_daily.
i tt_daily = tt(tmx, tmn, tb, topt, tmax).
x comparison (3) is possible only for atomic and list types
i The error occurred in row 1.
Thanks for any advice.
There is a typo in the function which should be tmax instead of max
tt<-function(tmx, tmn, tb, topt, tmax){
tmean<- (tmx + tmn) / 2
if(tmean <= tb) {t1 = 0}
if(tmean >tb & tmean <=topt) {t1 = tmean - tb}
if(tmean>topt & tmean<tmax) {t1 = (topt - tb) / (topt - tmax) * (tmean - tmax)}
if(tmean >= tmax) {t1 <- 0}
return(t1)
}
Now, we apply the function within mutate after appending the other arguments as a named list within pmap
library(dplyr)
library(purrr)
data1 %>%
mutate(tt_daily = pmap_dbl(c(across(tmx:tmn),
dplyr::lst(tb, topt, tmax)), tt))
-output
doy tmx tmn relHum srad tt_daily
1 148 31.3 13.8 68.3 30.4 11.55
2 149 31.1 17.2 62.2 30.0 13.15
3 150 30.1 16.1 69.7 20.9 12.10
4 151 27.3 16.2 77.1 26.1 10.75
5 152 33.4 18.4 65.9 27.4 14.90
6 153 27.2 18.0 70.3 26.6 11.60
7 154 30.3 13.0 71.5 28.4 10.65
8 155 36.2 22.0 62.2 28.8 18.10
9 156 32.9 22.2 61.1 24.9 16.55
10 157 30.5 16.2 63.2 27.9 12.35
11 158 25.7 19.3 71.0 18.3 11.50
12 159 29.1 18.3 87.2 12.7 12.70
13 160 28.5 20.3 70.2 24.8 13.40
Or using rowwise
data1 %>%
rowwise %>%
mutate(tt_daily = tt(tmx, tmn, tb, topt, tmax)) %>%
ungroup
-output
# A tibble: 13 x 6
doy tmx tmn relHum srad tt_daily
<int> <dbl> <dbl> <dbl> <dbl> <dbl>
1 148 31.3 13.8 68.3 30.4 11.6
2 149 31.1 17.2 62.2 30 13.2
3 150 30.1 16.1 69.7 20.9 12.1
4 151 27.3 16.2 77.1 26.1 10.8
5 152 33.4 18.4 65.9 27.4 14.9
6 153 27.2 18 70.3 26.6 11.6
7 154 30.3 13 71.5 28.4 10.6
8 155 36.2 22 62.2 28.8 18.1
9 156 32.9 22.2 61.1 24.9 16.5
10 157 30.5 16.2 63.2 27.9 12.4
11 158 25.7 19.3 71 18.3 11.5
12 159 29.1 18.3 87.2 12.7 12.7
13 160 28.5 20.3 70.2 24.8 13.4
If we want to add a new column, then it may be better to either return a list or tibble in 'tt' function
tt<-function(tmx, tmn, tb, topt, tmax){
tmean<- (tmx + tmn) / 2
if(tmean <= tb) {t1 = 0}
if(tmean >tb & tmean <=topt) {t1 = tmean - tb}
if(tmean>topt & tmean<tmax) {t1 = (topt - tb) / (topt - tmax) * (tmean - tmax)}
if(tmean >= tmax) {t1 <- 0}
return(tibble(tt_daily = t1, tmean = tmean))
}
Now, we wrap the contents in a list and unnest the output column
library(tidyr)
data1 %>%
rowwise %>%
mutate(out = list(tt(tmx, tmn, tb, topt, tmax))) %>%
ungroup %>%
unnest_wider(c(out))
# A tibble: 13 x 7
doy tmx tmn relHum srad tt_daily tmean
<int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 148 31.3 13.8 68.3 30.4 11.6 22.6
2 149 31.1 17.2 62.2 30 13.2 24.2
3 150 30.1 16.1 69.7 20.9 12.1 23.1
4 151 27.3 16.2 77.1 26.1 10.8 21.8
5 152 33.4 18.4 65.9 27.4 14.9 25.9
6 153 27.2 18 70.3 26.6 11.6 22.6
7 154 30.3 13 71.5 28.4 10.6 21.6
8 155 36.2 22 62.2 28.8 18.1 29.1
9 156 32.9 22.2 61.1 24.9 16.5 27.6
10 157 30.5 16.2 63.2 27.9 12.4 23.4
11 158 25.7 19.3 71 18.3 11.5 22.5
12 159 29.1 18.3 87.2 12.7 12.7 23.7
13 160 28.5 20.3 70.2 24.8 13.4 24.4
This question already has answers here:
R - reshaping 2 column data frame to multiple column matrix [duplicate]
(4 answers)
Reshaping 2 column data.table from long to wide
(2 answers)
Closed 5 years ago.
I'm quite the novice at R and I haven't been able to find an answer of how to split a column with multiple variables (sample 1-4) into separate columns whilst moving the data it correlates with. Here's an example:
Samples Content
Sample 1 70.7
Sample 1 91.6
Sample 1 92.6
Sample 1 65.2
Sample 1 80.0
Sample 1 82.1
Sample 1 88.1
Sample 1 92.2
Sample 1 53.3
Sample 1 80.0
Sample 1 60.3
Sample 1 89.7
Sample 1 84.8
Sample 1 94.0
Sample 1 71.8
Sample 1 76.9
Sample 1 91.4
Sample 1 57.9
Sample 1 61.9
Sample 1 71.5
Sample 2 88.7
Sample 2 67.6
Sample 2 61.7
Sample 2 70.8
Sample 2 45.3
Sample 2 55.6
Sample 2 64.6
Sample 2 62.7
Sample 2 72.4
Sample 2 46.8
Sample 2 59.0
Sample 2 63.7
Sample 2 67.0
Sample 2 71.6
Sample 2 48.3
Sample 2 55.6
Sample 2 62.5
Sample 2 60.0
Sample 2 72.9
Sample 2 47.4
Sample 3 42.3
Sample 3 48.2
Sample 3 64.0
Sample 3 33.3
Sample 3 19.0
Sample 3 41.0
Sample 3 53.1
Sample 3 46.5
Sample 3 30.0
Sample 3 43.4
Sample 3 43.7
Sample 3 92.0
Sample 3 53.0
Sample 3 33.0
Sample 3 48.4
Sample 3 43.2
Sample 3 41.8
Sample 3 62.5
Sample 3 33.3
Sample 3 49.3
Sample 4 51.8
Sample 4 57.3
Sample 4 43.3
Sample 4 42.3
Sample 4 37.6
Sample 4 54.9
Sample 4 71.1
Sample 4 33.8
Sample 4 43.1
Sample 4 39.1
Sample 4 63.0
Sample 4 74.0
Sample 4 31.0
Sample 4 48.3
Sample 4 42.9
Sample 4 62.2
Sample 4 35.4
Sample 4 33.8
Sample 4 40.7
Sample 4 41.2
I tried tidyr with no success. I want the output to be something like this;
Sample 1 Sample 2 Sample 3 Sample 4
70.7 88.7 42.3 51.8
91.6 67.6 48.2 57.3
92.6 61.7 64.0 43.3
65.2 70.8 33.3 42.3
80.0 45.3 19.0 37.6
82.1 55.6 41.0 54.9
88.1 64.6 53.1 71.1
92.2 62.7 46.5 33.8
53.3 72.4 30.0 43.1
80.0 46.8 43.4 39.1
60.3 59.0 43.7 63.0
89.7 63.7 92.0 74.0
84.8 67.0 53.0 31.0
94.0 71.6 33.0 48.3
71.8 48.3 48.4 42.9
76.9 55.6 43.2 62.2
91.4 62.5 41.8 35.4
57.9 60.0 62.5 33.8
61.9 72.9 33.3 40.7
71.5 47.4 49.3 41.2
Many thanks, if a solution is identified, is there an answer if I wanted to do the reciprocate?
Extra - Is there any way to preform a t-test on data which is stacked in one column such as the first example without having to transform it?
You may be having the "duplicate identifiers" issue using tidyr::spread. You need first to generate unique combinations of Sample + identifier, which you can do like this (assuming data frame named df1):
library(tidyverse) # for dplyr + tidyr
df1 %>%
group_by(Samples) %>%
mutate(id = row_number()) %>%
spread(Samples, Content) %>%
select(-id)
"if I wanted to do the reciprocate"
Do you mean go the other way, from the wide form back to the original long form? Then you use gather. Add this to the end of the code above and see what happens:
%>% gather(Samples, Content)
t-test: there are lots of ways you could run a t-test on the long format data. For example, a base R way to compare Samples 1 and 2 might be:
t.test(df1[df1$Samples == "Sample 1", "Content"],
df1[df1$Samples == "Sample 2", "Content"])
As the number of elements for each 'Sample' is the same, we can use unstack from base R
unstack(df1, Content~Samples)
# Sample.1 Sample.2 Sample.3 Sample.4
#1 70.7 88.7 42.3 51.8
#2 91.6 67.6 48.2 57.3
#3 92.6 61.7 64.0 43.3
#4 65.2 70.8 33.3 42.3
#5 80.0 45.3 19.0 37.6
#6 82.1 55.6 41.0 54.9
#7 88.1 64.6 53.1 71.1
#8 92.2 62.7 46.5 33.8
#9 53.3 72.4 30.0 43.1
#10 80.0 46.8 43.4 39.1
#11 60.3 59.0 43.7 63.0
#12 89.7 63.7 92.0 74.0
#13 84.8 67.0 53.0 31.0
#14 94.0 71.6 33.0 48.3
#15 71.8 48.3 48.4 42.9
#16 76.9 55.6 43.2 62.2
#17 91.4 62.5 41.8 35.4
#18 57.9 60.0 62.5 33.8
#19 61.9 72.9 33.3 40.7
#20 71.5 47.4 49.3 41.2
No external packages are used
If the number of 'Sample' elements are different, then dcast from data.table can be used (works in both cases)
library(data.table)
dcast(setDT(df1), rowid(Samples)~Samples, value.var = "Content")
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
I have the following data.
HEIrank1
HEI.ID X2007 X2008 X2009 X2010 X2011 X2012
1 OP 41.8 147.6 90.3 82.9 106.8 63.0
2 MO 20.0 20.8 21.1 20.9 12.6 20.6
3 SD 21.2 32.3 25.7 23.9 25.0 40.1
4 UN 51.8 39.8 19.9 20.9 21.6 22.5
5 WS 18.0 19.9 15.3 13.6 15.7 15.2
6 BF 11.5 36.9 20.0 23.2 18.2 23.8
7 ME 34.2 30.3 28.4 30.1 31.5 25.6
8 IM 7.7 18.1 20.5 14.6 17.2 17.1
9 OM 11.4 11.2 12.2 11.1 13.4 19.2
10 DC 14.3 28.7 20.1 17.0 22.3 16.2
11 OC 28.6 44.0 24.9 27.9 34.0 30.7
12 TH 7.4 10.0 5.8 8.8 8.7 8.6
13 CC 12.1 11.0 12.2 12.1 14.9 15.0
14 MM 11.7 24.2 18.4 18.6 31.9 31.7
15 MC 19.0 13.7 17.0 20.4 20.5 12.1
16 SH 11.4 24.8 26.1 12.7 19.9 25.9
17 SB 13.0 22.8 15.9 17.6 17.2 9.6
18 SN 11.5 18.6 22.9 12.0 20.3 11.6
19 ER 10.8 13.2 20.0 11.0 14.9 14.2
20 SL 44.9 21.6 21.3 26.5 17.0 8.0
I try following commends to draw regression line for each HEIs.
year <- c(2007 , 2008 , 2009 , 2010 , 2011, 2012)
op <- as.numeric(HEIrank1[1,])
lm.r <- lm(op~year)
plot(year, op)
abline(lm.r)
I want to draw to draw regression line for each college in one graph and I do not how.can you help me.
Here's my approach with ggplot2 but the graph is uninterpretable with that many lines.
library(ggplot2);library(reshape2)
mdat <- melt(HEIrank1, variable.name="year")
mdat$year <- as.numeric(substring(mdat$year, 2))
ggplot(mdat, aes(year, value, colour=HEI.ID, group=HEI.ID)) +
geom_point() + stat_smooth(se = FALSE, method="lm")
Faceting may be a better way to got:
ggplot(mdat, aes(year, value, group=HEI.ID)) +
geom_point() + stat_smooth(se = FALSE, method="lm") +
facet_wrap(~HEI.ID)
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)