Extract certain amount of factor levels in R - 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?

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 ), ]

Related

Create full data frame from possible combinations of grouping variables

I apologize if this has been asked before, but I could not find the answer I needed when there are three grouping variables.
I need to fill a dataframe with possible combinations of variables, but insert NAs for a non-grouping observation values when a combination does not appear. Say there is a dataframe with three grouping variables: Year, Geography, and Grouping:
Year <- rep(2008:2019,each=50)
Geography <- rep(1:60,each=10)
Grouping <- rep(1:4,each=150)
value <- seq(rnorm(600,mean=0,sd=1))
df=cbind(Year,Geography)
df=as.data.frame(cbind(df,value))
But the dataframe is missing some random observations like so:
df2=df[-c(15,60,150,510),]
How would one go about changing the dataframe back into a length of 600 (which is the length it would be if all possible combinations of three grouping variables were present), but inserting NAs where the value would be if the combinations were in the dataframe? Note that all unique observations for each grouping variable are present in the dataset at some point.

get all the combinations of 10 factor variables

I have 10 factor variables, i want to get all the possible unique combinations of the factor variables by level wise.
My dataframe has the following data variables:
And i want to get the output as formatted below:
unique(dataframe_name)
This command will display unique values in dataframe.
unique_data <- subset(unique(dataframe_name))

Transpose/Reshape Data in R

I have a data set in a wide format, consisting of two rows, one with the variable names and one with the corresponding values. The variables represent characteristics of individuals from a sample of size 1000. For instance I have 1000 variables regarding the size of each individual, then 1000 variables with the height, then 1000 variables with the weight etc. Now I would like to run simple regressions (say weight on calorie consumption), the only way I can think of doing this is to declare a vector that contains the 1000 observations of each variable, say for instance:
regressor1=c(mydata$height0, mydata$height1, mydata$height2, mydata$height3, ... mydata$height1000)
But given that I have a few dozen variables and each containing 1000 observations this will become cumbersome. Is there a way to do this with a loop?
I have also thought a about the reshape options of R, but this again will put me in a position where I have to type 1000 variables a few dozen times.
Thank you for your help.
Here is how I would go about your issue. t() will transpose the data for you from many columns to many rows.
Note: t() can be used with a matrix rather than a data frame, I simply coerced to data frame to show my example will work with your data.
# Many columns, 2 rows
x <- as.data.frame(matrix(nrow=2,ncol=1000,seq(1:2000)))
#2 Columns, many rows
t(x)
Based on your comments you are looking to generate vectors.
If you have transposed:
regressor1 <- x[,1]
regressor2 <- x[,2]
If you have not transposed:
regressor1 <- x[1,]
regressor2 <- x[2,]

keep most common factor levels in R

I used the "dummies" package to create 42 dummy variables for the 42 levels of a factor variable in my data-frame. Now I only want to keep the 5 dummies that represent the five most common factor levels. I used:
counts <- colSums(dummy_variables)
rank <- sort(counts)
to figure out what those levels are, but now I want to be able to reference the most common ones and keep them in my data frame. I am somewhat new to R - I just can't figure out the syntax to do this.
Filter out the top 5 variables, and then subset only those columns.
rank <- sort(counts)[(length(counts)-4):length(counts)]
dummy_variables <- dummy_variables[names(dummy_variables) %in% names(rank)]
Or in one line as the commenter suggested,
dummy_variables[names(dummy_variables) %in% names(tail(sort(colSums(dummy_variables)),5))]

Are there any packages that actually sample randomly without including the same data?

I have tried sample(), and srswor()/ srswr() from the sampling package, none of these will select from my vector of factors, x number of unique factor levels. Just as often as not, they return two factor levels that are the same, in amongst however many random samples I ask for. Is there a package or script that can randomly select factor levels, but where no two are the same?
To sample from the factor levels you can simply do:
sample(levels(factor_variable), 10)
This randomly samples 10 levels from the total amount of unique levels in factor_variable.

Resources