Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to condense a dataset with dates in R

I’m trying to take some data and clean it for end user visibility, however I’m new to R and can’t quite seem to figure out how to go about it. Also, this is my first post, so please let me know if there are any formatting or structural issues to the way I wrote this question.

What the data looks like right now:

name date reason
john 1/1/2022 late
john 1/2/2022 late
john 1/4/2022 absent
betty 1/3/2022 absent
betty 1/5/2022 no call
betty 1/7/2022 no call
kyle 1/3/2022 absent
kyle 1/5/2022 no call
kyle 1/7/2022 no call

I want to see if there’s a way to condense this so that for every name, you have the date and reason all on the same line. Like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

name date1 reason1 date2 reason2 date3 reason3
john 1/1/2022 late 1/2/2022 late 1/4/2022 absent
betty 1/3/2022 absent 1/5/2022 no call 1/7/2022 no call
kyle 1/3/2022 absent 1/5/2022 no call 1/7/2022 no call

Alternatively, I tried using dcast, but my code produced numbers instead of dates.

new db <- dcast(db, name ~ reason, fun.aggregate = list, value.var = "date")

What I wanted:

name late absent no call
john 1/1/2022,1/2/2022 1/4/2022
betty 1/3/2022 1/5/2022,1/7/2022
kyle 1/3/2022 1/5/2022,1/7/2022

What I got:

name late absent no call
john c(1620708300,1627236300) 1639328820 numeric(0)
betty numeric(0) 1612973940 c(1611937080, 1612455480)
kyle numeric(0) 1639329540 c(1635526800, 1639760400)

I must be missing a step somewhere, because it feels like I’m pretty close to getting what I want, it’s just a matter of learning, I suppose. Thanks in advance for any assistance!

>Solution :

Try this if you want to keep the observations combined

library(tidyr)

as.data.frame(pivot_wider(df, names_from=reason, values_from=date, 
  values_fn=list, values_fill=list("")))
   name               late   absent            no call
1  john 1/1/2022, 1/2/2022 1/4/2022                   
2 betty                    1/3/2022 1/5/2022, 1/7/2022
3  kyle                    1/3/2022 1/5/2022, 1/7/2022

Data

df <- structure(list(name = c("john", "john", "john", "betty", "betty", 
"betty", "kyle", "kyle", "kyle"), date = c("1/1/2022", "1/2/2022", 
"1/4/2022", "1/3/2022", "1/5/2022", "1/7/2022", "1/3/2022", "1/5/2022", 
"1/7/2022"), reason = c("late", "late", "absent", "absent", "no call", 
"no call", "absent", "no call", "no call")), class = "data.frame", row.names = c(NA, 
-9L))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading