0

I created a simple function that has ifelse, and would like to use multiple dataframe columns as its input. Here is what I did.

year <- c(2016, 2016, 2017, 2017)
month <- c(11, 12, 1, 2)
df <- data.frame(year, month)

test_1 <- function(n, year_2016 = FALSE, year_2017 = FALSE){
  return (  ifelse( (year_2016 == FALSE && year_2017 == FALSE), ifelse(n==24, 'a', 'b'), ifelse(year_2016 == TRUE, ifelse(n==24, 'c', 'd'), ifelse(n==24, 'e', 'f'))))
}

test_2 <- function(n, year_2016 = FALSE){
  return (ifelse(year_2016 == FALSE, ifelse(n==12, 'g', 'h'), ifelse(n==12, 'i', 'j')))
}

test_1 outputs only a single value from the first row, while test_2 outputs the vector. I was thinking both should be vectors.

If anyone can guide me why this is happening, that would be great. Also, if there's a better way to do this vector calculation, please advice.

Thanks!

=====================================

edit:

Sorry, that I didn't include the line actually running the function. I ran the two followings, which I was hoping to get vectors in both.

test_1(df$month, df$year == 2016, df$year == 2017)
test_2(df$month, df$year == 2016)

However, when I run it, I get the following.

test_1(df$month, df$year == 2016, df$year == 2017)
[1] "d"

test_2(df$month, df$year == 2016)
[1] "j" "i" "h" "h"
drJones
  • 1,233
  • 1
  • 16
  • 24
Julie
  • 11
  • 1
  • 1
    Both return a single value when I run them. You are also not using the data frame columns as inputs at all. Can you elaborate on what exactly you are trying to do? – drJones Nov 25 '17 at 06:23
  • please create a reproducible example so that we can work with it. your function doesn't really make sense. – Matt W. Nov 25 '17 at 10:09
  • Following your edit I would suggest replacing `&&` with `&`. The single ampersand will evaluate each element in the vector, see [here](https://stackoverflow.com/questions/6558921/r-boolean-operators-and) – drJones Nov 26 '17 at 04:56
  • Thank you @mr_jaundo, that was it. – Julie Nov 28 '17 at 15:05

1 Answers1

0

I have had to guess a bit here and there are many ways that you could improve this, but you can use this to understand how functions work better and how to use your data frame as input in the function.

year <- c(2016, 2016, 2017, 2017)
month <- c(11, 12, 1, 2)
df <- data.frame(year, month)

test_1 <- function(x){
  return (  ifelse( (x[1] != 2016 & x[1] != 2017), ifelse(x[2] == 12, 'a', 'b'), ifelse(x[1] == 2016, ifelse(x[2] == 24, 'c', 'd'), ifelse(x[2] == 24, 'e', 'f'))))
}

test_2 <- function(x){
  return (ifelse(x[1] != 2016, ifelse(x[2] == 12, 'g', 'h'), ifelse(x[2] == 12, 'i', 'j')))
}

##take each row of df and apply the functions
apply(df, 1, test_1)
[1] "d" "d" "f" "f"

apply(df, 1, test_2)
[1] "j" "i" "h" "h"

If you have any questions let me know

drJones
  • 1,233
  • 1
  • 16
  • 24