Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@MonkmanMH
Created July 5, 2017 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MonkmanMH/cdaad0112afc25fac8e1622eeca96303 to your computer and use it in GitHub Desktop.
Save MonkmanMH/cdaad0112afc25fac8e1622eeca96303 to your computer and use it in GitHub Desktop.
mutate alternate values
library(tidyverse)
datatab <- as.tibble(c(1:10))
# modulo division
datatab$value %% 2
# since we have alternating even and odd value in "value" variable
datatab %>%
mutate(valueplus = ifelse((value %% 2) == 0, "even", "odd"))
# for row numbers (i.e. 'rownames') can't put in a pipe
mutate(valueplus2 = ifelse((as.numeric(rownames(datatab)) %% 2) == 0, "even", "odd"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment