Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - How to determine if every value in column of dataframe is zero?

Tags:

dataframe

r

I have a dataframe and want to determine for a given column if every value in the column is equal to zero.

This is the code I have:

z <- read.zoo(sub, sep = ",", header = TRUE, index = 1:2, tz = "", format = "%Y-%m-%d %H:%M:%S")

if(all.equal(z$C_duration, 0)) 
  C_dur_acf = NA

But I am getting an error:

 Error in if (all.equal(z$C_duration, 0)) { : 
  argument is not interpretable as logical

The code should return a boolean value (TRUE/FALSE) if the entire column is all zeros.

like image 660
T.Grover Avatar asked Oct 24 '16 20:10

T.Grover


People also ask

How do you know if a DataFrame has zero values?

Select the Dataframe column by its name i.e., df['C']. Then apply a condition on it i.e. ( df['C']==0 ). It gives a bool Series object, where each True value indicates that the corresponding value in the column is zero.

How do you check all the values in a data frame?

Pandas DataFrame all() Method The all() method returns one value for each column, True if ALL values in that column are True, otherwise False. By specifying the column axis ( axis='columns' ), the all() method returns True if ALL values in that axis are True.


1 Answers

Use all builtin: all(z$C_duration == 0)

like image 50
Steves Avatar answered Oct 22 '22 00:10

Steves