jeudi 22 septembre 2016

If statement with nested subset function

I am trying to do a if statement with a subset function in it. I have a dataframe dat1, for example like this:

     Unit   Cost    Date
     1        40    Sep
     1        50    Dec
     2        55    Sep
     2        30    Oct

And based on its row nrow(dat1) I want to subset an other dataframe (dat2)

      unit  model   sales
      1      AAA    100
      1      BBB    110
      1      CCC    130
      4      ZZZ    120
      5      YYY    128

I wrote a ifstatement like this:

     Sales <- ifelse(nrow(dat1)>=30, 
            dat2[which(dat2$unit==1 & dat2$model=="AAA"),], 
            dat2[which(dat2$unit==1),])

So if the nrow>30 I want to apply subset on 2 dimensions of dat2, else just on one of them. However this gives me a list with only the first column, not a dataframe with all the 3 columns of dat2. What is the right command to do this? Thanks in advance for your help.

Aucun commentaire:

Enregistrer un commentaire