Snippets Collections
pivot_longer(
  data,
  cols, #to select based on name use starts_with("pattern_"),
  names_to = "name",
  names_prefix = NULL,
  names_sep = NULL,
  names_pattern = NULL,
  names_ptypes = list(),
  names_transform = list(),
  names_repair = "check_unique",
  values_to = "value",
  values_drop_na = FALSE,
  values_ptypes = list(),
  values_transform = list(col1 = as.integer
                         col2 = as.numeric),
)

### Deriving variables from complex column headers ###

df %>%
  pivot_longer (
  	cols,
	names_to = c("var1", ".value"),
	names_sep = "_")
df %>% 
  # Impute/fill the column
  fill(col_name, .direction = "up") # .direction can be "up" or "down"
# put multiple obs from 1 cell on to multiple rows

df %>%
  separate_rows(col_w_many_obs, sep = ", ") 


#LINK TO:




#########Separate############

df %>% 
  separate(column_w_2_obs, into = c("obs1", "obs2"), sep = ",") 
### use "convert = TRUE" after sep arg to convert separeted columns into numeric if applicable.

#########Unite############
df %>%
  unite(united_col, col1, col2, sep = " ") #Sep = " " makes white space



star

Thu Jun 02 2022 07:55:15 GMT+0000 (Coordinated Universal Time)

#r #tidyr
star

Thu Jun 02 2022 07:35:31 GMT+0000 (Coordinated Universal Time)

#r #tidyr
star

Thu Jun 02 2022 06:18:50 GMT+0000 (Coordinated Universal Time)

#r #tidyr
star

Thu Jun 02 2022 06:02:35 GMT+0000 (Coordinated Universal Time)

#r #tidyr

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension