0

I am currently understanding to use R for data cleaning and arranging purposes. I have a dataframe that looks like this:

Id    Shopping_date_1    Shopping_date_2    Shopping_date_3
1     01/05/2018         01/25/2018         NA
2     02/06/2019         NA                 NA
3     08/05/2018         NA                 01/04/2019

I want to arrange the dataframe such that i get a column that can count number of times a user has shopped something like this:

Id    Shopping_date_1    Shopping_date_2    Shopping_date_3    Shop_count
1     01/05/2018         01/25/2018         NA                 2
2     02/06/2019         NA                 NA                 1
3     08/05/2018         NA                 01/04/2019         2

Please help!

  • 1
    Related: [R: count NAs per row in dataframe](https://stackoverflow.com/questions/37801338/r-count-nas-per-row-in-dataframe) – markus Jul 09 '19 at 18:55

1 Answers1

2

You can do:

rowSums(!is.na(df[, 2:length(df)]))

[1] 2 1 2
tmfmnk
  • 38,881
  • 4
  • 47
  • 67