Science_blog: date

Search This Blog

Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Tuesday, 28 February 2023

How to insert date in row in R

 #########Import your file#########

> culti <- read.csv("cultivation2.csv", header = TRUE, sep = ",")

> culti$date <- as.Date(culti$hdate)

> cult2 <- data.frame(culti$date,   culti$site,    culti$code)

> colnames(cult2) <- c("date", "site", "crop")

> cutiec1 <- cult2 %>%

  filter(site == 1)


###########Do not forget to check your structure of data#######

> str(cutiec1) ("It should be character otherwise it would not work")

'data.frame': 11 obs. of  3 variables:

 $ date: Date, format: "2009-12-12" ...

 $ site: int  1 1 1 1 1 1 1 1 1 1 ...

 $ crop: chr  "CC" "SM" "WW" "WR" ...

####Change date to character format all rows

> cutiec1$date <- as.character(cutiec1$date)

> cutiec1$site <- as.character(cutiec1$site)

###option 1#####

> cutiec1[nrow(cutiec1)+ 1, ] <- c("2018-09-29")

but this would create problems like this 

date       site       crop

1  2009-08-02          1       <NA>

2  2009-12-12          1         CC

3  2010-10-14          1         SM

4  2011-07-28          1         WW

5  2012-07-20          1         WR

6  2013-08-04          1         WW

7  2013-12-11          1         CC

8  2014-10-09          1         SM

9  2015-07-22          1         WW

10 2016-11-01          1         GM

11 2017-07-30          1         WW

12 2018-07-09          1         WR

13 2018-09-29 2018-09-29 2018-09-29

Hence follow this ---

############Option 2##########

> cutiec1 <- cutiec1 %>% add_row(date="2009-08-02", site="1", .before = 1)

> cutiec1 <- cutiec1 %>% add_row(date="2018-09-29", site="1", .after = 12)

######then character date to POSIXct date

> cutiec1$date <- as.POSIXct(cutiec1$date, tz = "UTC")

#########Then convert POSIXct date to date format

> cutiec1$date <- as.Date(cutiec1$date)

Then do "pad". It will creat date from 2009-08-02 to 2018-09-29 at interval of day

> cutiec12 <- pad(cutiec1, interval = "day",)


It shows 3346 variables for all 9 years on daily basis.


#############################Done##############################################

Thursday, 16 February 2023

How to change date format in notepad++

 You can do this with notepad++:


< >

Find:  ([0-9]+)-+([0-9]+)-+([0-9]+) 


Replace: \3-\2-\1

Note: Careful with \3-\2-\1. It depends upon your date format what you have wrote in notepad++. It represents the potion of year, date and month 

#######

To make sure you only reorder wrong formats (in case you have mixed formats from merging databases), use this:


([0-9]{2})-+([0-9]{2})-+([0-9]{4})

This searches for (four digits, dash, two digits, dash, two digits).


In an regex capable editor like notepad++, replace it with this:


\3-\2-\1

In a tool like libre office, you need to replace it with this:


$3-$2-$1 

#############

Used Notepad++ to change mm/dd/yyyy to yyyy/mm/dd in several lines of a text file. Script was saved as a macro for next file.


Find: ([0-9]{2})/+([0-9]{2})/+([0-9]{4}) Replace: \3/\1/\2



#########

Capturing groups of various elements of an ISO date representation


"(\d{4})-(\d{1,2})-(\d{1,2})T([0-2]\d):([0-5]\d):([0-5]\d)(?:.\d+)?Z?\s?"


In total six groups are being captured in this pattern, seventh group in the end containing '?:' is non-capturing, meaning it does not keep any value recorded by the group, simply ignores them.


The six groups we have captured have numbers based on positions such as \1 \2 \3... upto \6.


Replacement pattern to change that date into Database supported date format mostly used in Oracle values like - "11/30/2010 00:00:00 AM"


"\2/\3/\1 \4:\5:\6 AM"


It works well with Notepad++ Good Luck!

Filter row wise data from data frame with dates in R

What Is the Best Way to Filter by Date in R?


Method 1: After Date Filter Rows


df %>% filter(date_column > '2022-01-01')


Method 2: Filter Rows Before Date


df %>% filter(date_column < '2022-01-01')


Method 3: Filter Rows Between Two Dates


df %>% filter(between(date_column, as.Date('2022-01-20'), as.Date('2022-02-20')))



Let’s create a data frame


df <- data.frame(day=seq(as.Date('2022-01-01'), by = 'week', length.out=10),

                 sales=c(40, 35, 39, 44, 48, 51, 23, 29, 60, 65))

Now we can view the data frame


df

          day sales

1  2022-01-01   240

2  2022-01-08   335

3  2022-01-15   359

4  2022-01-22   544

5  2022-01-29   548

6  2022-02-05   251

7  2022-02-12   223

8  2022-02-19   529

9  2022-02-26   660

10 2022-03-05   165


Example 1: Filter Rows After Date

To filter for rows in the data frame with a date after 1/25/2022, use the following code.


library(tidyverse)

filter for rows with dates after 1/25/2022


df %>% filter(day > '2022-01-25')

        day sales

1 2022-01-29   548

2 2022-02-05   251

3 2022-02-12   223

4 2022-02-19   529

5 2022-02-26   660

6 2022-03-05   165

Each row in the generated data frame has a date that is later than 1/25/2022.


Best Books on Data Science with Python – Data Science Tutorials


Example 2: Filter Rows Before Date

To filter for rows in the data frame with a date before 1/25/2022, we can use the following code.


library(dplyr)

Let’s filter for rows with dates before 1/25/2022


df %>% filter(day < '2022-01-25')

        day sales

1 2022-01-01   240

2 2022-01-08   335

3 2022-01-15   359

4 2022-01-22   544

Each entry in the generated data frame has a date that is prior to 1/25/2022.


Example 3: Filter Rows Between Two Dates

To filter for rows in the data frame with a date between 1/20/2022 and 2/20/2022, use the following code.


library(tidyverse)

filter for rows with dates between 1/20/2022 and 2/20/2022


Best Data Science YouTube Tutorials Free to Learn – Data Science Tutorials


df %>% filter(between(daty, as.Date('2022-01-20'), as.Date('2022-02-20')))

         day sales

1 2022-01-22   544

2 2022-01-29   548

3 2022-02-05   251

4 2022-02-12   223

5 2022-02-19   529

The dates in the rows of the generated data frame range from 1/20/2022 to 2/20/2022.


If none of the ways above work, you may need to use them as.Date() function to convert the dates you’re working with to a recognized date format.

Multidisciplinary Mega‑Journals: Has Their Time Passed?

     Over the past decade, multidisciplinary and so‑called “mega‑journals” became some of the most attractive destinations for researchers u...