Filter rows using rowSums

Let's say I have a dataframe : dfsales where subjectid is the name of the store and month1:month7 is the column representing sales for seven months where 0 = no sales, 1 = in-store sale, 2 = online sale and NA = no data(could be actual no data or the store was not open yet e.g for a and b for month 1,2)

  subjectid = c("a","b","c","d","e","f","g","h","i","j","k"),
  month1 = c(NA,NA,1,0,0,2,1,1,0,0,0),
  month2 = c(NA,NA,0,0,0,0,NA,0,0,0,NA),
  month3 = c(NA,1,0,1,0,0,0,1,NA,NA,NA),
  month4 = c(0,0,0,0,0,1,2,0,1,NA,0),
  month5 = c(NA,NA,NA,NA,NA,NA,0,1,1,2,0),
  month6 = c(NA,NA,0,0,0,NA,NA,0,0,0,0),
  month7 = c(0,0,0,0,0,0,NA,0,0,0,0))
dfsales
#>    subjectid month1 month2 month3 month4 month5 month6 month7
#> 1          a     NA     NA     NA      0     NA     NA      0
#> 2          b     NA     NA      1      0     NA     NA      0
#> 3          c      1      0      0      0     NA      0      0
#> 4          d      0      0      1      0     NA      0      0
#> 5          e      0      0      0      0     NA      0      0
#> 6          f      2      0      0      1     NA     NA      0
#> 7          g      1     NA      0      2      0     NA     NA
#> 8          h      1      0      1      0      1      0      0
#> 9          i      0      0     NA      1      1      0      0
#> 10         j      0      0     NA     NA      2      0      0
#> 11         k      0     NA     NA      0      0      0      0
Created on 2021-11-03 by the reprex package (v2.0.1)

I want to filter and delete those subjectid who have never had a sale for the entire 7 months (column month1:month7) and create a new dataset dfsalesonly. Example subjectid e and k who never has a value of 1 or 2 (i.e. no sales). I used something like this but did not work.

dfsalesonly <- filter(dfsales,rowSums(dfsales[,2:8])!= 0, na.rm = TRUE)

Can anyone point where I went wrong? Thanks

na.rm is wrongly positioned, it should be a parameter of rowSums rather than of filter

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.