Edward M.
Edward M.

Reputation: 221

Extract certain amount of factor levels in R

I have a data frame with a column with more than 100 factor levels.

I want to extract rows to make the column just have 50 factor levels, to decrease the calculation time.

How to randomly extract certain amount of factor levels?

Upvotes: 0

Views: 465

Answers (1)

G5W
G5W

Reputation: 37661

To avoid no answer ...

You can use sample to get a random sample of the factor and then use %in% to select the relevant rows of your data.frame.

ReducedFactors = sample(levels(df$MyFactor), 50) 
df[which(df$MyFactor %in% ReducedFactors ), ]

Upvotes: 2

Related Questions