Count and aggregate by date [duplicate] - r

This question already has answers here:
Convert date-time string to class Date
(4 answers)
Count number of rows within each group
(17 answers)
Closed 1 year ago.
The dataset:
Date
2021-09-25T17:07:24.222Z
2021-09-25T16:17:20.376Z
2021-09-24T09:30:53.013Z
2021-09-24T09:06:24.565Z
I would like to count the number of rows per day. For example, 2021-09-25 will be 2.
To solve said challenge I looked at the following post:
Count and Aggregate Date in R
The answer of Rorshach is the solution. However, I do not understand how I can format my rows in the Date column to 2021/09/24 instead of 2021-09-24T09:06:24.565Z.
Could someone explain to me how to format the entries in the Date column?

After converting the date you may use table to count occurrence of each Date.
table(as.Date(df$Date))
#2021-09-24 2021-09-25
# 2 2

Parse the string into a datetime object and then extract the date (without the hours and minutes) to be able to count:
library(dplyr)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
tibble::tribble(
~Date,
"2021-09-25T17:07:24.222Z",
"2021-09-25T16:17:20.376Z",
"2021-09-24T09:30:53.013Z",
"2021-09-24T09:06:24.565Z"
) %>%
mutate(
day = Date %>% parse_datetime() %>% as.Date()
) %>%
count(day)
#> # A tibble: 2 × 2
#> day n
#> <date> <int>
#> 1 2021-09-24 2
#> 2 2021-09-25 2

#RonakShah's answer is good, but to have the dataframe in better format, use the count function from the plyr library:
library(plyr)
count(as.Date(df$Date))
Output:
x freq
1 2021-09-24 2
2 2021-09-25 2

Related

Join numbers together in R [duplicate]

This question already has answers here:
Paste multiple columns together
(11 answers)
Closed 7 months ago.
I'm a beginner to R. What I want to do is join numbers together. I made a data as follows:
data<-data.frame(year=c(2020,2021,2022),month=c(10,11,12))
My expected output is as follows:
data=data.frame(year=c(2020,2021,2022),month=c(10,11,12),year_month=c(202010,202111,202212))
year_month is the column joining year and month together.
How can I do this?
You could concatenate the columns using paste0 like this:
data<-data.frame(year=c(2020,2021,2022),month=c(10,11,12))
data$year_month <- do.call(paste0, data)
data
#> year month year_month
#> 1 2020 10 202010
#> 2 2021 11 202111
#> 3 2022 12 202212
Created on 2022-07-30 by the reprex package (v2.0.1)

Create date of "X" column, when I have age in days at "X" column and birth date column in R

I'm having some trouble finding out how to do a specific thing in R.
In my dataset, I have a column with the date of birth of participants. I also have a column giving me the age in days at which a disease was diagnosed.
What I want to do is to create a new column showing the date of diagnosis. I'm guessing it's a pretty easy thing to do since I have all the information needed, basically it's birth date + X number of days = Date of diagnosis, but I'm unable to figure out how to do it.
All of my searches give me information on the opposite, going from date to age. So if you're able to help me, it would be much appreciated!
library(tidyverse)
library(lubridate)
df <- tibble(
birth = sample(seq("1950-01-01" %>%
as.Date(),
today(), by = "day"), 10, replace = TRUE),
age = sample(3650:15000, 10, replace = TRUE)
)
df %>%
mutate(diagnosis_date = birth %m+% days(age))
#> # A tibble: 10 x 3
#> birth age diagnosis_date
#> <date> <int> <date>
#> 1 1955-01-16 6684 1973-05-05
#> 2 1958-11-03 6322 1976-02-24
#> 3 2007-02-23 4312 2018-12-14
#> 4 2002-07-11 8681 2026-04-17
#> 5 2021-12-28 11892 2054-07-20
#> 6 2017-07-31 3872 2028-03-07
#> 7 1995-06-30 14549 2035-04-30
#> 8 1955-09-02 12633 1990-04-04
#> 9 1958-10-10 4534 1971-03-10
#> 10 1980-12-05 6893 1999-10-20
Created on 2022-06-30 by the reprex package (v2.0.1)

Sum table values ​per day

I have a table as shown in the image, where each comment has a publication date, with year, month, day and time, I would like to add the sentiment values ​​by day.
this is how the table is composed
serie <- data.frame(comments$created_time,sentiment2$positive-sentiment2$negative)
Using dplyr you can do:
library(dplyr)
df %>%
group_by(as.Date(comments.created_time)) %>%
summarize(total = sum(sentiment))
Here is some sample data that will help others to troubleshoot and understand the data:
df <- tibble(comments.created_time = c("2015-01-26 22:43:00",
"2015-01-26 22:44:00",
"2015-01-27 22:43:00",
"2015-01-27 22:44:00",
"2015-01-28 22:43:00",
"2015-01-28 22:44:00"),
sentiment = c(1,3,5,1,9,1))
Using the sample data will yield:
# A tibble: 3 × 2
`as.Date(comments.created_time)` total
<date> <dbl>
1 2015-01-26 4
2 2015-01-27 6
3 2015-01-28 10

Question Regarding creating time period/intervals in R?

I have an issue with creating a time period in R.
The data that I have on hand is enter image description here
Now I want to do the following.
Identify the hour intervals between start and end time and create a list
Identify the hour intervals b/n start and the break time
Finally, remove the break intervals to find the total time
and then create an output.
Could you please assist me?
I am not entirely sure what exactly you need, but from what i understand here is something to get you going:
library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
t <- tibble(
place = "xyz",
st = "9:00",
et = "13:00"
)
t <- t %>%
mutate(stdur = period_to_seconds(hm(st))) %>%
mutate(etdur = period_to_seconds(hm(et))) %>%
mutate(interval = dseconds(etdur - stdur)) %>%
mutate(interval_hours = seconds_to_period(interval))
glimpse(t)
#> Observations: 1
#> Variables: 7
#> $ place <chr> "xyz"
#> $ st <chr> "9:00"
#> $ et <chr> "13:00"
#> $ stdur <dbl> 32400
#> $ etdur <dbl> 46800
#> $ interval <Duration> 14400s (~4 hours)
#> $ interval_hours <Period> 4H 0M 0S
Created on 2020-02-10 by the reprex package (v0.3.0)

Tableau LOD R Equivalent

I'm using a Tableau Fixed LOD function in a report, and was looking for ways to mimic this functionality in R.
Data set looks like:
Soldto<-c("123456","122456","123456","122456","124560","125560")
Shipto<-c("123456","122555","122456","124560","122560","122456")
IssueDate<-as.Date(c("2017-01-01","2017-01-02","2017-01-01","2017-01-02","2017-01-01","2017-01-01"))
Method<-c("Ground","Ground","Ground","Air","Ground","Ground")
Delivery<-c("000123","000456","000123","000345","000456","000555")
df1<-data.frame(Soldto,Shipto,IssueDate,Method,Delivery)
What I'm looking to do is "For each Sold-to/Ship-to/Method count the number of unique delivery IDs".
The intent is to find the number of unique deliveries that could potentially be "aggregated."
In Tableau that function looks like:
{FIXED [Soldto],[Shipto],[IssueDate],[Method],:countd([Delivery])
Could this be done with aggregate or summarize as in an example below:
df.new<-ddply(df,c("Soldto","Shipto","Method"),summarise,
Deliveries = n_distinct(Delivery))
This is fairly easy with dplyr. You are looking for the number of unique delivery for each combination of soldto, shipto and method, which is just group_by and then summarise:
library(tidyverse)
tbl <- tibble(
soldto = c("123456","122456","123456","122456","124560","125560"),
shipto = c("123456","122555","122456","124560","122560","122456"),
issuedate = as.Date(c("2017-01-01","2017-01-02","2017-01-01","2017-01-02","2017-01-01","2017-01-01")),
method = c("Ground","Ground","Ground","Air","Ground","Ground"),
delivery = c("000123","000456","000123","000345","000456","000555")
)
tbl %>%
group_by(soldto, shipto, method) %>%
summarise(uniques = n_distinct(delivery))
#> # A tibble: 6 x 4
#> # Groups: soldto, shipto [?]
#> soldto shipto method uniques
#> <chr> <chr> <chr> <int>
#> 1 122456 122555 Ground 1
#> 2 122456 124560 Air 1
#> 3 123456 122456 Ground 1
#> 4 123456 123456 Ground 1
#> 5 124560 122560 Ground 1
#> 6 125560 122456 Ground 1
Created on 2018-03-02 by the reprex package (v0.2.0).

Resources