0

I am trying to create a function that will take an input of character strings "mm/did/yyyy" and return an output vector in numeric form the month, day and year. I essentially want to combine this new function with the weekday() function created below to ultimately return the day of the week the input character string corresponds with.

weekday<-function(q,r,s)
{ (if(q>= 3)
 m<-(q-2)
else
m<-(q+10))
k<-r
 c<-floor(s/100)
(if(q>=3)
y<-s%%100
else
y<-(s%%100)-1)
 f<-(floor((2.6*m)-0.2)+k+y+floor(y/4)+floor(c/4)-(2*c))%%7
if(f==0){return("Sunday")}
   else 
if(f==1){return("Monday")}
 else
 if(f==2){return("Tuesday")}
 else
 if(f==3){return("Wednesday")}
 else
  if(f==4){return("Thursday")}
 else
  if(f==5){return("Friday")}
else
  if(f==6){return("Saturday")}}

I tried using something along the lines of type.convert but this isn't producing the desired output. Any help would be great thanks!

 dateconvert<-function("q/r/s")
  {
    type.convert(dateconvert(), na.strings = )

    weekday(convertedanswer)
    Return (weekday)


 }
2
  • R has functions to convert character string to POSIX or datetime format then you can get either months, weekdays, days, year, or time from that datetime format. Why dont you use it? Commented Oct 6, 2017 at 3:04
  • Oddly, there is a plural version of your user built function that is included in base R: ?weekdays. Commented Oct 6, 2017 at 11:08

3 Answers 3

1

Have you tried lubridate package?

input <- "12/30/2017"

# change into as.date format
inputdate <- strptime(input, "%m/%d/%Y")

library("lubridate")
day(inputdate)
# [1] 30
month(inputdate)
# [1] 12
year(inputdate)
# [1] 2017

It seems like a roundabout way to get to the day of week though. You should try using wday() that comes with lubridate package.

wday(inputdate, label=T)
# [1] Sat
# Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat. 
# as a Ordered factor (Sunday is first)

wday(inputdate)
# [1] 7 
# wday returns the day of the week as a decimal number (01-07, Sunday is 1) or an .
Sign up to request clarification or add additional context in comments.

Comments

1

Alternatively without package:

date = "06/10/2017"
POSIXdate = as.POSIXlt(date, format = "%d/%m/%Y")
strftime(POSIXdate, "%A")
#Friday

# Or if you  like one-liner
strftime(as.POSIXlt("dd/mm/yyyy", format= "%d/%m/%Y"), "%A")

3 Comments

Also weekdays in base R.
Is there any way to do this so that a function created newfunction<-("10/06/17") would produce the vector 10 06 17 ? Ideally I would like for this to happen, so that I can put this result into my weekday() function above.
Do you want to have input of character d <- "10/06/17" and output a vector of character c("10" , "06", "17")? If so: strsplit("10/06/17", "/")[[1]].
1

The lubridate package is great for this! You can use lubridate::mdy() to convert a date in the "mm/dd/yyyy" formate you mentioned, and then lubridate::week() to get the week.

lubridate::week(lubridate::mdy("10/05/2017"))
#> 5

If you want the day itself, rather than the numeric output, you can use

lubridate::wday(lubridate::mdy("10/05/2017"),
                label = TRUE)
#> [1] Thurs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.