r - If statement with nested subset function -


i trying if statement subset function in it. have dataframe dat1, example this:

     unit   cost    date      1        40    sep      1        50    dec      2        55    sep      2        30    oct 

and based on row nrow(dat1) want subset other dataframe (dat2)

      unit  model   sales       1      aaa    100       1      bbb    110       1      ccc    130       4      zzz    120       5      yyy    128 

i wrote ifstatement this:

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

so if nrow>30 want apply subset on 2 dimensions of dat2, else on 1 of them. gives me list first column, not dataframe 3 columns of dat2. right command this? in advance help.

this works:

sales <- dat2[which(dat2$unit==1),] # default if (nrow(dat1)>=30) {     sales <-dat2[which(dat2$unit==1 & dat2$model=="aaa"),] } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -