I want to count in R how many rows I have that have no 0 value at all

44 Views Asked by At

I have a data set with 5908 rows (now the variables) and 5 columns (observations). The variables take the value 0.00 in some column. I want to sort out the rows that do not have any zero values at all.

df <- read.delim2("permutation.txt")

# Randomly select 5 column names out of 60 (because I actually have 60 columns)
selected_column_names <- sample(colnames(df), 5)


# Select the 5 specified columns from the data frame
selected_data <- df[, selected_column_names]

I would like the rows that do not contain any 0.00 value at all to be sorted out.

1

There are 1 best solutions below

0
Near Lin On

Apply a function to each row and save the result into a variable.

selected_data$drop <- apply(testdf, 1, \(x) any(x == 0))

Then you can filter your dataset based on the variable. Like:

testdf[!testdf$drop, ]

It is also possible to count how many FALSE in drop if only the number is needed.