T.Grover T.Grover - 1 month ago 8
R Question

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

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.

Answer

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

Comments