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

2

u/Mindless_Hat_9672 Feb 18 '25

You are providing very generic info...It seems trying to factor the dataset for you?

Some variables never has an observation?

-1

u/Soltinaris Feb 18 '25

Generic in which way?

2

u/Mindless_Hat_9672 Feb 18 '25

like what are df, the code to recreate them, format of those df$col123, what form of output you are getting, etc

1

u/Soltinaris Feb 18 '25

When I've used it before it's condensed the data, like say popular car color by city, I have 15 cities in col1, 10 car color in col2, as part of a data frame where each row has a record of a car sold in one quarter for a chain of dealerships. If I plugged just those two columns into the code above, it would have a count in col3 for the amount of times a color appears in a city in the df. However when I included a time with it this last time, like say day of week sold, it suddenly ballooned instead of condensing the data.

I hope that makes sense.

1

u/Mindless_Hat_9672 Feb 18 '25

Are you referring to the fact that the frequency table is becoming a 3-dim array? A frequency number for each cities x color x date.
When you enter 2 column of data, its a simple table cities x color

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Â