-1

I have a data frame with 4 columns. I want to add a fith column with values from a function that uses the values of the row in in data frame as an input.

I have tried the following for example...

my.function <-(input1,input2,input3){
random = sample(10:200,1)
if((input1>=5) & (input2>=15) & (input3>=35)){
(input1 * input2 * input3) + random
}else{
(input1+input2+input3) + random
}

}
}

my.frame <- (1:10,11:20,21:30,31:40)
my.frame$new.col <- my.function(my.frame[,1],my.frame[,2],my.frame[,3])

...but this does not work.

What am I doing wrong?

EDIT: I have added an example for the function. It should now be reproducible.

user3347232
  • 407
  • 1
  • 7
  • 16
  • I don't see a reproducible example, so I can offer only guesses. Possibly you want `mapply`. – Roland Sep 09 '14 at 13:04
  • I am not sure if I want apply at all. `my.frame$new.col <- my.function(my.frame[,1],my.frame[,2],my.frame[,2])` returns a warning message from my function that does not appear when using in in other instances. `In if ((input1 >= numeric_value1) & (input2 >= numeric_value2) & (input3 >= numeric_value3) {: the condition has length > 1 and only the first element will be used` Is the error in the condition of my function? – user3347232 Sep 09 '14 at 13:17
  • As Roland hinted, read https://stackoverflow.com/help/mcve and http://stackoverflow.com/q/5963269/134830 and update your question, or prepare for downvotes and possible closing of your question. – Richie Cotton Sep 09 '14 at 13:38

2 Answers2

0

In the absence of a reproducible example, some standard pointers. To add an extra column to a data frame based upon existing columns, you want to use with, within or mutate (from the plyr package).

if takes a single logical value as an input. The warning you saw happened because you passed it a vector. You probably want ifelse instead.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • I have added an example function to my sample code. If I execute the last line of code I get the following error message: `In if((input1>=5) & (input2>=15) & (input3>=35)) { : the condition has length > 1 and only the first element will be used` How do I exchange this to ifelse? – user3347232 Sep 09 '14 at 20:02
  • Start by reading the help page, `?ifelse`, and running the examples, `example(ifelse)`. You may also want to read the `?Logic` help page. – Richie Cotton Sep 10 '14 at 05:16
0

Well one quick thing...the way you have your example written my.function is not a function. It may be a typo but you need to declare functions "as a function" such as:

my.function <- function(x,y,z){ }

Another quick point is your my.frame has the same kind of problem as your function definition again may be a typo but it looks most like you are trying to declare a vector without using a "c" as in c(1:10,11:20), but I know that's not what you are trying to do. I think you want a data.frame in that case:

my.frame <- data.frame(a=1:10,b=11:20,c=21:30,d=31:40)

Finally, as mentioned above you are testing a vector condition so you need a vectored conditional operator. I believe ifelse is vector(ized) too but you could just replace the & with && (two ampersands). I made the small changes to your code I mention above and it worked. Not sure what you are trying to do so it may or may not accomplish that. Cheers

miles2know
  • 737
  • 8
  • 17