Remove variables with factor level 1

r

Solution

Use this:

x[, sapply(x, nlevels) > 1]

Problem

I am using the program `gs` in the `bnlearn` package for my data frame `EMGbin`. The dataframe `EMGbin` contains all factors, ranging from A to Z. `EMGbin` has 600000 columns and 130 rows. Here is a sample of `EMGbin`: ``` V101 V102 V103 V104 V105 V106 1 L M D S O O 2 L M C P A O 3 J M C O O O 4 L N D R A O 5 K M D O A O 6 K M C P O O 7 K N D Q O O 8 L N D R O O 9 L M D O O O 10 K M D S A O ``` When I run the program `gs(EMGbin)`, I get the error: `Error in check.data(x) : all factors must have at least two levels.` When I run `sapply(EMGbin, nlevels)`, I see the levels of factors each of the 600,000 variables has, and I see some of them are listed as 1 level. Would removing the variables with 1 factor level help? So far, the only way I know how to do this is `x[, sapply(x, fun) != 1]`, but I don't know what to substitute in for `fun`.

Original source