Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206278

Is there a way to mutate and create many new variables with one line of code?

$
0
0

I have something like:

df<-data.frame(group=c(1, 1, 1, 1,1,2, 2, 2, 2), 
               date=c("2000-01-01 11:00:00", "2000-01-03 11:00:00", "2000-01-04 11:20:00", "2000-01-04 14:20:00", "2000-01-05 11:40:00", "2000-01-09 12:20:00", "2000-01-09 13:20:00", "2000-01-10 12:20:00", "2000-01-12 16:20:00"))
  group                date
1     1 2000-01-01 11:00:00
2     1 2000-01-03 11:00:00
3     1 2000-01-04 11:20:00
4     1 2000-01-04 14:20:00
5     1 2000-01-05 11:40:00
6     2 2000-01-09 12:20:00
7     2 2000-01-09 13:20:00
8     2 2000-01-10 12:20:00
9     2 2000-01-12 16:20:00

I'd like to make many columns indicating 24 hours after the date, 48 hours, etc. (for instance):

df%>%mutate(first=date+86400, second=date+172800, third=date+259200) 

etc. etc. where I'm adding a day in seconds, but this is very time consuming (if I want hundreds of columns). I'm assuming there's a way to do this iteratively.

Thanks,


Viewing all articles
Browse latest Browse all 206278

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>