I have this data.frame with a lot of NAs:
df <- data.frame(a = rep(letters[1:3], each = 3),
b = c(NA, NA, NA, 1, NA, 3, NA, NA, 7))
df
> df
a b
1 a NA
2 a NA
3 a NA
4 b 1
5 b NA
6 b 3
7 c NA
8 c NA
9 c 7
I would like to subset this dataframe to obtain only factor group rows that have no less than two values, such as this:
a b
1 b 1
2 b NA
3 b 3
I have tried this function but it doesn't work:
subset(df, sum(!is.na(b)) < 1, by = a)
> [1] a b
<0 rows> (or 0-length row.names)
Any suggestion? (other packages solutions are welcome)
We can use
data.table
. Convert the 'data.frame' to 'data.table' (setDT(df)
), grouped by 'a',if
thesum
of logical vector (i.e. non-NA elements -!is.na(b)
) is greater than 1, then Subset the Data.table.Or using
dplyr
, with the same logic, after grouping by 'a', wefilter
the rows.Or in
base R
withave
One way is using
aggregate
. Counting the number of elements which are notNA
for a every uniquea
and then sub setting those rows from the data frame.Another option is using
table
.