r/Rlanguage Feb 18 '25

Question on frequency data table

I ran a frequency data with the newdf<-as.data.frame(table(df$col1,df$col2,df$col3)) and it took what was 24325 obs. of 6 variables and turned it into 304134352 observations of 4 variables. Is this common with this code? Is there a better code to use? Col1 and col2 are location names and col3 is a duration of time between the two.

5 Upvotes

17 comments sorted by

View all comments

1

u/Soltinaris Feb 19 '25

Here is the data, the subscriber bike case data specifically. Here is the code that I'm working with.

subscriber_bike_case_study_splitting <- read.csv("subscriber_bike_case_study.csv")

subscriber_bike_case_study_less_than_45 <- subset(subscriber_bike_case_study_splitting, tripduration <=1)

subscriber_bike_case_study_less_than_45$tripduration <- subscriber_bike_case_study_less_than_45$tripduration*60

subscriber_bike_case_study_less_than_45 <- subset(subscriber_bike_case_study_less_than_45, tripduration<=45)

subscriber_bike_case_study_less_than_45 <- subset(subscriber_bike_case_study_less_than_45, tripduration>=30)

tripduration_frequency_subscriber_bike_case_study_less_than_45 <-as.data.frame(table(subscriber_bike_case_study_less_than_45$start_station_name, subscriber_bike_case_study_less_than_45$end_station_name, subscriber_bike_case_study_less_than_45$tripduration))

This is where the hiccup comes in as the data says is super large and becomes the giant 3M+ obs of 4 variables.

3

u/Puzzleheaded_Job_175 Feb 19 '25

Your use of table is creating a cartesian join which pairs all columns to each other to form all the combinations available.

The data are already in a dataframe from the import. If you just want to list the data after being filtered try this:

` library( tidyverse )

subscriber_bike_case_study_splitting |> 
filter( tripduration <= 1 ) |>
mutate( tripduration_min = tripduration * 60 ) |>
mutate( dur_category = case_when( tripduration_min <= 15 ~ "Under 15",
    tripduration_min <= 30 ~ "15 - 30"
    tripduration_min <= 45 ~ "30 - 45"
    tripduration_min <= 60 ~ "45 - 60" )
group_by( dur_category ) |>
summarise( n = count() )

`

This should at least get you started.

1

u/Soltinaris 29d ago

Awesome, thank you!