Skip to content

Instantly share code, notes, and snippets.

@rasmusto
Created May 19, 2016 22:01
Show Gist options
  • Save rasmusto/afbfc28416432301e180e473bbcbc7ba to your computer and use it in GitHub Desktop.
Save rasmusto/afbfc28416432301e180e473bbcbc7ba to your computer and use it in GitHub Desktop.
require(dplyr) # version 0.4.3
f1 <- function(x, y) { if (x < 5) y else if (x >= 5) y + 10 }
f2 <- function(x, y) { if (x < 5) y + 0 else if (x >= 5) y + 10 }
d <- data.frame(x=1:10, y=1:10)
d %>% rowwise() %>% mutate(z=f1(x, y))
# Error: incompatible types, expecting a integer vector
d %>% rowwise() %>% mutate(z=f2(x, y))
# Source: local data frame [10 x 3]
# Groups: <by row>
#
# x y z
# (int) (int) (dbl)
# 1 1 1 1
# 2 2 2 2
# 3 3 3 3
# 4 4 4 4
# 5 5 5 15
# 6 6 6 16
# 7 7 7 17
# 8 8 8 18
# 9 9 9 19
# 10 10 10 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment