Skip to content

Instantly share code, notes, and snippets.

@mine-cetinkaya-rundel
Last active April 17, 2018 17:56
Show Gist options
  • Save mine-cetinkaya-rundel/3620fbcb5e50497815a4f9167f71b15f to your computer and use it in GitHub Desktop.
Save mine-cetinkaya-rundel/3620fbcb5e50497815a4f9167f71b15f to your computer and use it in GitHub Desktop.
library(tidyverse)
# list col for age --------------------------------------------------
df_listcol <- tibble(
age = list(30, 40, 50, 55, "65+")
)
typeof(df_listcol$age)
#> [1] "list"
## filter out the weird obs, remainder can be converted to double
df_listcol_sub <- df_listcol %>%
filter(age != "65+") %>%
mutate(age = as.numeric(age))
typeof(df_listcol_sub$age)
#> [1] "double"
## recode the weird obs, remainder can be converted to double
df_listcol_recode <- df_listcol %>%
mutate(
age = ifelse(age == "65+", 65, age),
age = as.numeric(age)
)
typeof(df_listcol_recode$age)
#> [1] "double"
# char col for age --------------------------------------------------
df_charcol <- tibble(
age = c(30, 40, 50, 55, "65+")
)
typeof(df_charcol$age)
#> [1] "character"
df_charcol_sub <- df_charcol %>%
filter(age != "65+") %>%
mutate(age = as.numeric(age))
typeof(df_charcol_sub$age)
#> [1] "double"
df_charcol_recode <- df_charcol %>%
mutate(
age = ifelse(age == "65+", 65, age),
age = as.numeric(age)
)
typeof(df_charcol_recode$age)
#> [1] "double"
#' Created on 2018-04-17 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment