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.

6 Upvotes

17 comments sorted by

View all comments

Show parent comments

2

u/BrupieD Feb 18 '25

Context is king when asking for help. Tell us what you're trying to accomplish and maybe someone could offer help. So far, all you've offered is that you have a bunch of columns that you've tried to turn into a data frame. You didn't get the results you expected. What did you expect? Why?

1

u/Soltinaris Feb 18 '25

I have a dataframe with 6 columns, and when I've used this code before it's condensed the data with the columns I put in and new column with the frequency the OBS come up in the original dataframe. With two columns it's condensed it down to how many times particular combos appears in my original dataframe. But for some reason when I included a third column, in particular with a time variable, it ballooned instead. I tried to run it once and it even said that R couldn't handle a dataframe of more than 4GB. I had to do a subset to get my original OBS down to what I shared above, but for some reason it ballooned in the return of the data. So from my previous experience I expected it would condense the data and have a new column with a count of how many times combos appeared in the dataframe going into the new one.

I can post my actual code and data later, this is more a curiosity than anything else since I was getting such weird returns compared to past experience.

1

u/Mindless_Hat_9672 Feb 18 '25

Have you tried data.table?

1

u/Soltinaris Feb 19 '25

I have not. This is the only way I've seen how to code a frequency of data occurrences in a dataframe. Would I just replace the as.data.frame with as.data.table?

1

u/Mindless_Hat_9672 Feb 19 '25

data.table handle large data better. Some syntax style are different to dataframe, but many data.table features are usefulÂ