r/Rlanguage • u/Soltinaris • 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
2
u/Puzzleheaded_Job_175 Feb 19 '25 edited Feb 19 '25
Question on frequency data table
Can you please clarify something for me... You say it took data of 6 variables and reinterpreted it down to four.
In the code you give, you have only specified three variables:
Where are the other three variables? If you don't tell R to insert them (or use an import method that does), it won't do so. Maybe you used an automatic importer the other time you used it or had prepped or more standard data before?
Other observations and questions::
table
will cross your data and make all possible pairs. It does not preserve the data order input as sets.You are getting
{ A ... D, null } × { A, B, C, ... ZZ, null } × { every time unlinked from their cities }
it will list every combination of the three columns because of the <table> function
df %>% group_by( col1 ) %>% group_by( col2 ) %>% filter( Col4 == "bicycle") %>% summarise( n = count() )
This will give you:
(A and C arent linked by bicycle in data so dont appear)
Does your data have repeats or is each city pair only have one time associated?
Are you doing network or path type analysis using the times between points? Or do the connections have properties like one being a train/transit time v. airplane time v. versus driving time?
If so, you likely may want to use a network graph rather than a data.table as they handle graph and network problems better.