Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove R Dataframe rows based on zero values in one column [duplicate]

I have this dataframe with a set of zero values along two columns. df

 Sl_No  No_of_Mails No_of_Responses
 1       10           2
 2        0           0
 3       20           0
 4       10          10
 5        0           0
 6        0          NA
 7       10          NA
 8       10           0

I want to remove those rows where No_of_Mails equals zero without disturbing the other column. I have tried the following code

row_sub = apply(df, 1, function(row) all(row !=0 ))
df[row_sub,]

This removes all the 0 values including the one from the number_of_responses column. I wish to have that column undisturbed I have also tried this

 df[df$No_of_Mails][!(apply(df, 1, function(y) any(y == 0))),].

This deletes all the rows and gives me a table with zero rows.

like image 781
Raghavan vmvs Avatar asked Apr 22 '26 11:04

Raghavan vmvs


1 Answers

Just subset the data frame based on the value in the No_of_Mails column:

df[df$No_of_Mails != 0, ]

Demo

like image 130
Tim Biegeleisen Avatar answered Apr 25 '26 02:04

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!