Week Number Calculation and if then condition

Hi All,

Here am looking to create week number from date column. Week number for dated 9/2/2019 as 36th week.

Date:-
9/1/2019
9/5/2019
9/11/2019
9/16/2019
9/20/2019
9/26/2019
9/30/2019

Can any one help me how to write code for Weeknumber.

I have a character column which is URL

//Community/document/pen
//Community/document/Pencil
//Community/document/Sharpner

I want to create a new column called page
If URL="//Community/document/pen" then Page=Pen
If UR:="//Community/document/Pencil" then Page =Pencil
else Page=Sharpner
End;

This i wanted to do in R programming Please help me to write code

This looks like two separate questions. Please ask only one question per thread.
To calculate the week of a date, there are three functions in the lubridate package. They use different definitions for the start of the first week in the year. Please see the help section in lubridate for an explanation.

I used the mdy() function from lubridate to change the text strings into Dates.

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:lubridate':
#> 
#>     intersect, setdiff, union
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
DF <- data.frame(Date = mdy(c("9/1/2019",
                          "9/5/2019",
                          "9/11/2019",
                          "9/16/2019",
                          "9/20/2019",
                          "9/26/2019",
                          "9/30/2019")))

DF <- DF %>% mutate(Week = week(Date), ISOweek = isoweek(Date), EPIweek = epiweek(Date))
#> Warning: The `printer` argument is deprecated as of rlang 0.3.0.
#> This warning is displayed once per session.
DF
#>         Date Week ISOweek EPIweek
#> 1 2019-09-01   35      35      36
#> 2 2019-09-05   36      36      36
#> 3 2019-09-11   37      37      37
#> 4 2019-09-16   37      38      38
#> 5 2019-09-20   38      38      38
#> 6 2019-09-26   39      39      39
#> 7 2019-09-30   39      40      40

Created on 2019-10-09 by the reprex package (v0.3.0.9000)

Hi Team,

Thanks for the reply. However am not able to recall the library after installing it trowing below error.
"Error in library(lubridate) : there is no package called ‘lubridate’"

Do you get any error message while installing lubridate?

install.packages('lubridate') 

Hi

Error in library(lubridate) : there is no package called ‘lubridate’
Error in library(lubridate) : there is no package called ‘lubridate’

install.packages("lubridate")
install.packages("lubridate")
Installing package into ‘C:/Users/Documents/R/win-library/3.3’
(as ‘lib’ is unspecified)
Installing package into ‘C:/Users/Documents/R/win-library/3.3’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/lubridate_1.7.4.zip'
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/lubridate_1.7.4.zip'
Content type 'application/zip' length 1354289 bytes (1.3 MB)
Content type 'application/zip' length 1354289 bytes (1.3 MB)
downloaded 1.3 MB

downloaded 1.3 MB

It's that the whole message? You are not getting the success confirmation so it seems like the package has not being installed.

Even after entering right code for installing package its showing the same message.

Install.package ('lubridate')

If you cannot install lubridate you can still extract it using base R. Of course, the same thing as with lubridate, what do you consider a week?. Base R easily extract the EPIweek and ISOweek too:

test <- data.frame(Date = c("9/1/2019","9/5/2019","9/11/2019","9/16/2019","9/20/2019","9/26/2019","9/30/2019"), stringsAsFactors = FALSE)
## make it POSIXlt
test$Date <- strptime(test$Date, '%m/%d/%Y')
## extract the week
test$week1 <- as.numeric(format(test$Date, "%U")) +1
test$week2 <- as.numeric(format(test$Date, "%W")) +1
test
        Date week1 week2
1 2019-09-01    36    35
2 2019-09-05    36    36
3 2019-09-11    37    37
4 2019-09-16    38    38
5 2019-09-20    38    38
6 2019-09-26    39    39
7 2019-09-30    40    40

Try this: If you are having problems installing lubridate on Windows, then try to close all RStudio/R session down and open RStudio again, so you start from scratch. Closing down R sometimes help as Windows locks folders in use.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.