I have a database export of user ids and dates logged in.
structure(list(User.Id = c(2542573L, 2571394L, 2770912L, 2683246L,
2832110L, 2773277L), Days.Played = c("", "2020-01-15,2020-01-16,2020-01-21,2020-01-22",
"2020-06-29", "2020-04-19,2020-04-24,2020-04-29", "2020-09-04",
"2020-06-23"), row.names = c(NA,
6L), class = "data.frame")
|---------------------|------------------|
| id | logged_in |
|---------------------|------------------|
| a | 2019-11-21, |
| | 2019-11-22, |
| | 2019-11-23,|
| | 2019-11-24,|
| | 2019-11-25 |
|---------------------|------------------|
| b | |
|---------------------|------------------|
| c | 2019-11-21, |
| | 2019-11-22, |
|---------------------|------------------|
What I am trying to do is split the date column by "," so each date is in it's own column
I want it to look like the below where there is a loggedin.[a:zz] stretching as wide as the longest string in the database. This could go to 1000 or more.
|---------------------|------------------|------------------|
| id | logged_in.a | loggedin.b |
|---------------------|------------------|------------------|
| a | 2019-11-21, | 2019-11-22 |
| | | |
| | | |
| | | |
| | | |
|---------------------|------------------|------------------|
| b | | |
|---------------------|------------------|------------------|
| c | 2019-11-21, | |
| | | 2019-11-22, |
|---------------------|------------------|------------------|
I then plan on gathering the dataset into a tall file. The code I used is below but I have to define the col names. My issue is I don't know how many there will be.
require(tidyr)
test %>% transform(.,Days.Played=colsplit(Days.Played, pattern=",", names=c('a','b')))
Does anyone know how to get around this issue or have any suggestions?
In
base R
, we can usestrsplit
withstack
-output
data