E B E B - 11 days ago 7
R Question

in R how to efficiently check values of columns in dataframe to be ALL Zeros

I have a dataframe like below:

Profit Title MonthText OCT JAN AUG SEP APR MAR MonthInd

13 -11821431 Alone in the Dark JAN 0 1 0 0 0 0 1
21 78114471 Are We There Yet? JAN 0 1 0 0 0 0 1
27 19944017 Be Cool MAR 0 0 0 0 0 1 1
28 13351350 Beauty Shop MAR 0 0 0 0 0 1 1
29 18508485 Because of Winn-Dixie FEB 0 0 0 0 0 0 0
38 47192859 Boogeyman FEB 0 0 0 0 0 0 0


I wanted to change the MonthInd for the whole dataframe based on the values of the columns OCT JAN AUG SEP APR MAR

So if any of the column values (OCT JAN AUG SEP APR MAR) is 1 then MonthInd = 0 else all of the column values (OCT JAN AUG SEP APR MAR) is 0 then MonthInd = 1

I tried row by row and this is what I would like to be apply to run this test : For example for row 1 ,

(all(test[1,c(4:9)] == 0 ) )


But I would like to apply it to all the rows in test without doing a for-loop for n rows

Is that possible?

42- 42-
Answer

I think your interest is in a row-by-row result for MonthInd and that you are promising that the values will be 0 or positive

test[ , "MonthInd"] <- 
      rowSums( test[ , c('OCT','JAN', 'AUG', 'SEP', 'APR', 'MAR')] ) > 0

#----
> test
      Profit                 Title MonthText OCT JAN AUG SEP APR MAR MonthInd
13 -11821431     Alone in the Dark       JAN   0   1   0   0   0   0     TRUE
21  78114471     Are We There Yet?       JAN   0   1   0   0   0   0     TRUE
27  19944017               Be Cool       MAR   0   0   0   0   0   1     TRUE
28  13351350           Beauty Shop       MAR   0   0   0   0   0   1     TRUE
29  18508485 Because of Winn-Dixie       FEB   0   0   0   0   0   0    FALSE
38  47192859             Boogeyman       FEB   0   0   0   0   0   0    FALSE

The other option would to be running apply with a anonymous function using any ( ... == 1).