1

I have the following piece of code:

TB <- data.frame(1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9)


TB$BIG_value <- apply(TB,1,function(r){
 sr <- sort(r[9:14],decreasing = TRUE)
 value <- sr[1] - 0.5*sr[3] - 0.25*sr[4] - 0.125*sr[5] - 0.0625*sr[6]
 defaltv <- data.frame(c(1))
 if((value==0)[1,1]{
   defaltv
 }else{
   value
 }
})

It's aim is to create a new column in TB, the value of which is either a function of columns 9:14 or 1, if the function returns 0.

If I run the code outside of the apply it works, however when inside it returns several errors saying Error: 'unexpected '}' in " }"'.

Thanks

Tania
  • 285
  • 1
  • 4
  • 13
  • `[1,1]` what does this do on `if` row? Also, if there is only one function following then no need for parentheses `{}`. – zx8754 Nov 28 '13 at 11:31

1 Answers1

3

You have unbalanced parentheses in:

if((value==0)[1,1]{
NPE
  • 486,780
  • 108
  • 951
  • 1,012