How do I recycle a character vector in R?How to make a great R reproducible exampleR: reshaping a dataframe and creating proportionsIdentify and cbind multiple vectors based on vector nameHow to divide contents of one column by different values, conditional on contents of a second column?Build column of data frame with character vectors of different length?Applying udfs on a data drameLoop a data frame so that a unique variable is repeated 7 times corresponding to the day of the week in Rerror message: do not know how to convert 'dataFrame$col' to class “Date”Averages for each row in my dataframe by a condition and put on a new dataframeaggregating/joining data by week with differing week lengths (5days vs 7days)
Why does the U.S. tolerate foreign influence from Saudi Arabia and Israel on its domestic policies while not tolerating that from China or Russia?
Keep milk (or milk alternative) for a day without a fridge
Is the genetic term "polycistronic" still used in modern biology?
Graduate student with abysmal English writing skills, how to help
What's the minimum number of sensors for a hobby GPS waypoint-following UAV?
Why was hardware diversification an asset for the IBM PC ecosystem?
What was the definition of "set" that resulted in Russell's Paradox
How are mathematicians paid to do research?
How to convert a file with several spaces into a tab-delimited file?
Price discrimination?
Why can a destructor change the state of a constant object?
How can I get a player to accept that they should stop trying to pull stunts without thinking them through first?
Do you know your 'KVZ's?
Does Lufthansa weigh your carry on luggage?
Is there a strong legal guarantee that the U.S. can give to another country that it won't attack them?
What's the point of having a RAID 1 configuration over incremental backups to a secondary drive?
How to properly say "bail on somebody" in German?
Fivenum and a little bit
How to know if blackberries are safe to eat?
Shortest hex dumping program
Optimization terminology: "Exact" v. "Approximate"
Can the Mage Hand cantrip be used to trip an enemy who is running away?
If your plane is out-of-control, why does military training instruct releasing the joystick to neutralize controls?
Why doesn't sea level show seasonality?
How do I recycle a character vector in R?
How to make a great R reproducible exampleR: reshaping a dataframe and creating proportionsIdentify and cbind multiple vectors based on vector nameHow to divide contents of one column by different values, conditional on contents of a second column?Build column of data frame with character vectors of different length?Applying udfs on a data drameLoop a data frame so that a unique variable is repeated 7 times corresponding to the day of the week in Rerror message: do not know how to convert 'dataFrame$col' to class “Date”Averages for each row in my dataframe by a condition and put on a new dataframeaggregating/joining data by week with differing week lengths (5days vs 7days)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a list of every day from 2018-01-01 to 2018-06-01. It is a vector and it looks like this:
dates <- c("2018-01-01", "2018-01-02", "2018-01-03", ... , "2018-05-30", "2018-06-01")
I want to make a data frame where the first column has each of those dates and the second column has their day of the week. I am assuming that 2018-01-01 is a Monday.
date day
2018-01-01 Monday
2018-01-02 Tuesday
2018-01-03 Wednesday
... ...
2018-06-01 Monday
I'm working on a data frame towards that end, but I was curious for a better way to recycle through the days of the week than the solution I put together.
day <- NULL
for (i in 1:length(dates))
x <- i
while (x > 7)
x <- i - 7
day <- c(day, days[x])
cbind(dates,day)
r
add a comment |
I have a list of every day from 2018-01-01 to 2018-06-01. It is a vector and it looks like this:
dates <- c("2018-01-01", "2018-01-02", "2018-01-03", ... , "2018-05-30", "2018-06-01")
I want to make a data frame where the first column has each of those dates and the second column has their day of the week. I am assuming that 2018-01-01 is a Monday.
date day
2018-01-01 Monday
2018-01-02 Tuesday
2018-01-03 Wednesday
... ...
2018-06-01 Monday
I'm working on a data frame towards that end, but I was curious for a better way to recycle through the days of the week than the solution I put together.
day <- NULL
for (i in 1:length(dates))
x <- i
while (x > 7)
x <- i - 7
day <- c(day, days[x])
cbind(dates,day)
r
add a comment |
I have a list of every day from 2018-01-01 to 2018-06-01. It is a vector and it looks like this:
dates <- c("2018-01-01", "2018-01-02", "2018-01-03", ... , "2018-05-30", "2018-06-01")
I want to make a data frame where the first column has each of those dates and the second column has their day of the week. I am assuming that 2018-01-01 is a Monday.
date day
2018-01-01 Monday
2018-01-02 Tuesday
2018-01-03 Wednesday
... ...
2018-06-01 Monday
I'm working on a data frame towards that end, but I was curious for a better way to recycle through the days of the week than the solution I put together.
day <- NULL
for (i in 1:length(dates))
x <- i
while (x > 7)
x <- i - 7
day <- c(day, days[x])
cbind(dates,day)
r
I have a list of every day from 2018-01-01 to 2018-06-01. It is a vector and it looks like this:
dates <- c("2018-01-01", "2018-01-02", "2018-01-03", ... , "2018-05-30", "2018-06-01")
I want to make a data frame where the first column has each of those dates and the second column has their day of the week. I am assuming that 2018-01-01 is a Monday.
date day
2018-01-01 Monday
2018-01-02 Tuesday
2018-01-03 Wednesday
... ...
2018-06-01 Monday
I'm working on a data frame towards that end, but I was curious for a better way to recycle through the days of the week than the solution I put together.
day <- NULL
for (i in 1:length(dates))
x <- i
while (x > 7)
x <- i - 7
day <- c(day, days[x])
cbind(dates,day)
r
r
asked Mar 26 at 2:20
CauderCauder
17411 bronze badges
17411 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
We can use weekdays to get day of the week and put it in a dataframe.
data.frame(dates, day = weekdays(dates))
# dates day
#1 2018-01-01 Monday
#2 2018-01-02 Tuesday
#3 2018-01-03 Wednesday
#4 2018-05-30 Wednesday
#5 2018-06-01 Friday
EDIT
If we don't want to use any in-built function we can create a vector of days and lookup from there. Considering the first day is "Monday" we can use the modulo operator to find the relevant day for rest of the dates
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
day <- days[(as.numeric(dates - dates[1]) %% 7) + 1]
day
#[1] "Monday" "Tuesday" "Wednesday" "Wednesday" "Friday"
and then put them in dataframe
data.frame(dates, day)
# dates day
#1 2018-01-01 Monday
#2 2018-01-02 Tuesday
#3 2018-01-03 Wednesday
#4 2018-05-30 Wednesday
#5 2018-06-01 Friday
data
dates<-as.Date(c("2018-01-01","2018-01-02","2018-01-03","2018-05-30","2018-06-01"))
For reasons related to the project, I'm not allowed to use any 'magic functions'
– Cauder
Mar 26 at 2:28
1
@Cauder how do you define a 'magic function' ?
– Ronak Shah
Mar 26 at 2:30
2
I'm limited to just 'dplyr' and base packages.
– Cauder
Mar 26 at 2:41
@Cauder I have updated the answer. I hope this does not fall into your category of "magic function".weekdaysis a base function.
– Ronak Shah
Mar 26 at 2:41
2
it's called modulo operator, it gives the remainder after division. Try12 %% 7,12 %% 6.
– Ronak Shah
Mar 26 at 2:51
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348953%2fhow-do-i-recycle-a-character-vector-in-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
We can use weekdays to get day of the week and put it in a dataframe.
data.frame(dates, day = weekdays(dates))
# dates day
#1 2018-01-01 Monday
#2 2018-01-02 Tuesday
#3 2018-01-03 Wednesday
#4 2018-05-30 Wednesday
#5 2018-06-01 Friday
EDIT
If we don't want to use any in-built function we can create a vector of days and lookup from there. Considering the first day is "Monday" we can use the modulo operator to find the relevant day for rest of the dates
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
day <- days[(as.numeric(dates - dates[1]) %% 7) + 1]
day
#[1] "Monday" "Tuesday" "Wednesday" "Wednesday" "Friday"
and then put them in dataframe
data.frame(dates, day)
# dates day
#1 2018-01-01 Monday
#2 2018-01-02 Tuesday
#3 2018-01-03 Wednesday
#4 2018-05-30 Wednesday
#5 2018-06-01 Friday
data
dates<-as.Date(c("2018-01-01","2018-01-02","2018-01-03","2018-05-30","2018-06-01"))
For reasons related to the project, I'm not allowed to use any 'magic functions'
– Cauder
Mar 26 at 2:28
1
@Cauder how do you define a 'magic function' ?
– Ronak Shah
Mar 26 at 2:30
2
I'm limited to just 'dplyr' and base packages.
– Cauder
Mar 26 at 2:41
@Cauder I have updated the answer. I hope this does not fall into your category of "magic function".weekdaysis a base function.
– Ronak Shah
Mar 26 at 2:41
2
it's called modulo operator, it gives the remainder after division. Try12 %% 7,12 %% 6.
– Ronak Shah
Mar 26 at 2:51
|
show 2 more comments
We can use weekdays to get day of the week and put it in a dataframe.
data.frame(dates, day = weekdays(dates))
# dates day
#1 2018-01-01 Monday
#2 2018-01-02 Tuesday
#3 2018-01-03 Wednesday
#4 2018-05-30 Wednesday
#5 2018-06-01 Friday
EDIT
If we don't want to use any in-built function we can create a vector of days and lookup from there. Considering the first day is "Monday" we can use the modulo operator to find the relevant day for rest of the dates
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
day <- days[(as.numeric(dates - dates[1]) %% 7) + 1]
day
#[1] "Monday" "Tuesday" "Wednesday" "Wednesday" "Friday"
and then put them in dataframe
data.frame(dates, day)
# dates day
#1 2018-01-01 Monday
#2 2018-01-02 Tuesday
#3 2018-01-03 Wednesday
#4 2018-05-30 Wednesday
#5 2018-06-01 Friday
data
dates<-as.Date(c("2018-01-01","2018-01-02","2018-01-03","2018-05-30","2018-06-01"))
For reasons related to the project, I'm not allowed to use any 'magic functions'
– Cauder
Mar 26 at 2:28
1
@Cauder how do you define a 'magic function' ?
– Ronak Shah
Mar 26 at 2:30
2
I'm limited to just 'dplyr' and base packages.
– Cauder
Mar 26 at 2:41
@Cauder I have updated the answer. I hope this does not fall into your category of "magic function".weekdaysis a base function.
– Ronak Shah
Mar 26 at 2:41
2
it's called modulo operator, it gives the remainder after division. Try12 %% 7,12 %% 6.
– Ronak Shah
Mar 26 at 2:51
|
show 2 more comments
We can use weekdays to get day of the week and put it in a dataframe.
data.frame(dates, day = weekdays(dates))
# dates day
#1 2018-01-01 Monday
#2 2018-01-02 Tuesday
#3 2018-01-03 Wednesday
#4 2018-05-30 Wednesday
#5 2018-06-01 Friday
EDIT
If we don't want to use any in-built function we can create a vector of days and lookup from there. Considering the first day is "Monday" we can use the modulo operator to find the relevant day for rest of the dates
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
day <- days[(as.numeric(dates - dates[1]) %% 7) + 1]
day
#[1] "Monday" "Tuesday" "Wednesday" "Wednesday" "Friday"
and then put them in dataframe
data.frame(dates, day)
# dates day
#1 2018-01-01 Monday
#2 2018-01-02 Tuesday
#3 2018-01-03 Wednesday
#4 2018-05-30 Wednesday
#5 2018-06-01 Friday
data
dates<-as.Date(c("2018-01-01","2018-01-02","2018-01-03","2018-05-30","2018-06-01"))
We can use weekdays to get day of the week and put it in a dataframe.
data.frame(dates, day = weekdays(dates))
# dates day
#1 2018-01-01 Monday
#2 2018-01-02 Tuesday
#3 2018-01-03 Wednesday
#4 2018-05-30 Wednesday
#5 2018-06-01 Friday
EDIT
If we don't want to use any in-built function we can create a vector of days and lookup from there. Considering the first day is "Monday" we can use the modulo operator to find the relevant day for rest of the dates
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
day <- days[(as.numeric(dates - dates[1]) %% 7) + 1]
day
#[1] "Monday" "Tuesday" "Wednesday" "Wednesday" "Friday"
and then put them in dataframe
data.frame(dates, day)
# dates day
#1 2018-01-01 Monday
#2 2018-01-02 Tuesday
#3 2018-01-03 Wednesday
#4 2018-05-30 Wednesday
#5 2018-06-01 Friday
data
dates<-as.Date(c("2018-01-01","2018-01-02","2018-01-03","2018-05-30","2018-06-01"))
edited Mar 26 at 2:37
answered Mar 26 at 2:24
Ronak ShahRonak Shah
66.3k10 gold badges47 silver badges80 bronze badges
66.3k10 gold badges47 silver badges80 bronze badges
For reasons related to the project, I'm not allowed to use any 'magic functions'
– Cauder
Mar 26 at 2:28
1
@Cauder how do you define a 'magic function' ?
– Ronak Shah
Mar 26 at 2:30
2
I'm limited to just 'dplyr' and base packages.
– Cauder
Mar 26 at 2:41
@Cauder I have updated the answer. I hope this does not fall into your category of "magic function".weekdaysis a base function.
– Ronak Shah
Mar 26 at 2:41
2
it's called modulo operator, it gives the remainder after division. Try12 %% 7,12 %% 6.
– Ronak Shah
Mar 26 at 2:51
|
show 2 more comments
For reasons related to the project, I'm not allowed to use any 'magic functions'
– Cauder
Mar 26 at 2:28
1
@Cauder how do you define a 'magic function' ?
– Ronak Shah
Mar 26 at 2:30
2
I'm limited to just 'dplyr' and base packages.
– Cauder
Mar 26 at 2:41
@Cauder I have updated the answer. I hope this does not fall into your category of "magic function".weekdaysis a base function.
– Ronak Shah
Mar 26 at 2:41
2
it's called modulo operator, it gives the remainder after division. Try12 %% 7,12 %% 6.
– Ronak Shah
Mar 26 at 2:51
For reasons related to the project, I'm not allowed to use any 'magic functions'
– Cauder
Mar 26 at 2:28
For reasons related to the project, I'm not allowed to use any 'magic functions'
– Cauder
Mar 26 at 2:28
1
1
@Cauder how do you define a 'magic function' ?
– Ronak Shah
Mar 26 at 2:30
@Cauder how do you define a 'magic function' ?
– Ronak Shah
Mar 26 at 2:30
2
2
I'm limited to just 'dplyr' and base packages.
– Cauder
Mar 26 at 2:41
I'm limited to just 'dplyr' and base packages.
– Cauder
Mar 26 at 2:41
@Cauder I have updated the answer. I hope this does not fall into your category of "magic function".
weekdays is a base function.– Ronak Shah
Mar 26 at 2:41
@Cauder I have updated the answer. I hope this does not fall into your category of "magic function".
weekdays is a base function.– Ronak Shah
Mar 26 at 2:41
2
2
it's called modulo operator, it gives the remainder after division. Try
12 %% 7, 12 %% 6.– Ronak Shah
Mar 26 at 2:51
it's called modulo operator, it gives the remainder after division. Try
12 %% 7, 12 %% 6.– Ronak Shah
Mar 26 at 2:51
|
show 2 more comments
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348953%2fhow-do-i-recycle-a-character-vector-in-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown