Arrange by Dates in a tibble

May I please have help sorting the tibble by date, when I attempt to sort using Days, I get the error message shown below.

library(readr)
library(tidyverse)
library(lubridate)
library(gt)
Lotto_winnings_2002to2022 <- read.csv("https://data.ny.gov/api/views/5xaw-6ayf/rows.csv?accessType=DOWNLOAD")

# Winning_Numbers is the string converted to array
# Needs more explanation of array, and unlist

Winning_Numbers <- list()
x = 1
while (x < nrow(Lotto_winnings_2002to2022)) {
  Winning_Numbers[[x]] <- as.numeric(array(unlist(strsplit(Lotto_winnings_2002to2022[x,2]," "))))
  x <- x + 1
}

# Using mdy() from lubridate package to convert
# 1st column of type string to type Date

Days = list()
y = 1
while (y< nrow(Lotto_winnings_2002to2022)) {
  Days[[y]] <- mdy(Lotto_winnings_2002to2022[y,1])
  y <- y + 1
}

MegaBall = list()
z = 1
while (z< nrow(Lotto_winnings_2002to2022)) {
  MegaBall[z] <- Lotto_winnings_2002to2022[z,3]
  z <- z + 1
}

Multiplier = list()
f = 1
while (f< nrow(Lotto_winnings_2002to2022)) {
  Multiplier[f] <- Lotto_winnings_2002to2022[f,4]
  f <- f + 1
}

# Create new Dataframe using converted columns
Lotto_Converted <- tibble(Days, Winning_Numbers, MegaBall, Multiplier)
Lotto_Converted_sorted <- arrange(Lotto_Converted, Lotto_Converted[1])
# summary of lottery rules needs to be added

ERROR MESSAGE as image and typed

Error in (function (..., na.last = TRUE, decreasing = FALSE, method = c("auto",  : 
  unimplemented type 'list' in 'orderVector1'
Error: VECTOR_ELT() can only be applied to a 'list', not a 'closure'
> View(Lotto_Converted)
Error in (function (..., na.last = TRUE, decreasing = FALSE, method = c("auto",  : 
  unimplemented type 'list' in 'orderVector1'
Error: VECTOR_ELT() can only be applied to a 'list', not a 'closure'
Error in .rs.findDataFrame("", "Lotto_Converted", "CB5482C2", "C:/Users/IanAK/AppData/Local/RStudio/viewer-cache") : 
  R_Reprotect: only 4 protected items, can't reprotect index -1
Error in .rs.findDataFrame("", "Lotto_Converted", "CB5482C2", "C:/Users/IanAK/AppData/Local/RStudio/viewer-cache") : 
  R_Reprotect: only 4 protected items, can't reprotect index -1

I do not understand what you are trying to do here. The data already seems to be sorted by Draw.Date in ascending order.

Also the Winning numbers look wierd. When I look at the data I downloaded each value is a simple character data point like this 46 54 57 58 66 and on the screenshot it looks like c(46 54 57 58 66).

Perhaps you could tell us what you are trying to do at the level of the project or analysis. Something like, I am out to break the NY lottery system and need the data in XX format?

Though Draw.Date appears to be in descending order, the data is from 2002 to 2022, it appears later in the table.

The Winning Numbers from the source are of type character, which was changed to type array(numeric)

Winning_Numbers <- list()
x = 1
while (x < nrow(Lotto_winnings_2002to2022)) {
  Winning_Numbers[[x]] <- as.numeric(array(unlist(strsplit(Lotto_winnings_2002to2022[x,2]," "))))
  x <- x + 1
}

There are 2 overall goals:

  1. To prepare a table
    2, To see if there are any patterns in the winning numbers, aka see if it is truely random

Ah, I see. only looked at a bit of the data. My appologies.

See if this is somewhere near what you want.


library(tidyverse)
library(lubridate)

dat1<- read.csv("https://data.ny.gov/api/views/5xaw-6ayf/rows.csv?accessType=DOWNLOAD")

dat1$Draw.Date  <- mdy(dat1$Draw.Date)

dat2  <- arrange(dat1, Draw.Date)

dat2 <- dat1 %>% separate(Winning.Numbers, c("d1", "d2", "d3", "d4" , "d5"), remove = TRUE)

dat3  <- dat2  %>%  as.tibble()  %>%  
  mutate_if(., is.character, as.numeric)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.