mercredi 30 août 2017

r - lapply, ifelse and trying to compare row vs previous row in one column

Forgive me in advance for trying to use my excel logic in R, but I can't seem to figure this out. In a function, given X I am trying to find out if the row prior to it has a greater value or not using simple logic. If it is, show in the new column as "yes" if not "no".

Here is the sample data:

temp <- data
GetFUNC<- function(x){
         temp <- cbind(temp, NewCol = ifelse(temp[2:nrow(temp),8] > temp[1:(nrow(temp)-1),8], "yes","no"))
}
lapply <- GetFUNC(test)

Just so you can see column 8 it looks like this:

testdata$volume
 [1] 32216510 10755328  8083097  6878500  8377025  6469979 10675856  8189887  5337239
[10]  5156737

The error:

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 11, 10

Thanks for any insight you can provide!

David

Aucun commentaire:

Enregistrer un commentaire