Jd S
Jd S

Reputation: 15

Extracted data frame selection still contains entries from full data frame set

I have a data frame (originally from a CSV file) with the columns NAME and YEAR. I have extracted a sample from this data frame of the first ten entries like so:

sample<-df(1:10,)

I want to know the frequency of the values in the NAME column so I input the following:

as.data.frame(table(sample$NAME))

This counts the frequency in the sample correctly but also includes every name from the original data frame in the 'Var1' column (all with a Freq of 0).

The same thing happens if I use unique(sample$NAME) as well: it lists the names from the sample along with all of the names from the original data frame as well.

What am I doing wrong?

Upvotes: 1

Views: 36

Answers (1)

akrun
akrun

Reputation: 886998

This could be a case of unused level in the 'NAME' factor column. We can use droplevels or call factor again to remove those unused levels.

as.data.frame(table(droplevels(sample$NAME)))

Or

as.data.frame(table(factor(sample$NAME)))

Upvotes: 0

Related Questions