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"))
write.csv(temp, file = paste0(x,".csv"))
}
lapply(example,GetFUNC)
Just so you can see column 8 it looks like this:
testdata$numbers
[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!
There are several problems:
lapplysince all the operations you are using are already vectorized.:binds more tightly than-(see?Syntax) so1:(nrow(temp)-1means(1:(nrow(temp))-1. You want1:(nrow(temp)-1)For example, compare these:even if the last one is corrected your
ifelseexpression returns a vector which is one smaller than the number of rows in testdata. Add on an NA at the beginning.1) Even better would be this assuming the input data frame is
testdataand defined as in the Note at the end:giving:
2) The above is likely what you want but here is a second solution using rollapplyr in the zoo package. It takes a rolling window of length 2 and performs a diff on each one filling the first value with NA.
Note: The input
testdatain reproducible form is: