misc:r_tips

# R tips

## Search for variables

• When data contain so many variables, you may want to search for variable names using some keywords. Here's a function I defined:
# Function to search variables
search_var <- function(df, pattern, ...) {
loc <- grep(pattern, names(df), ...)
if (length(loc) == 0) warning("There are no such variables")
else return(data.frame(loc = loc, varname = names(df)[loc]))
}

This function searches for variables that matches to argument pattern and return their location. For example in iris data, search for variables with "Length":

library(tidyverse)
iris %>%
search_var("Length")
  loc      varname
1   1 Sepal.Length
2   3 Petal.Length

An ellipsis … pass arguments to grep() function. For example, if you want to search instead for "length" ignoring lower/upper cases:

iris %>%
search_var("length", ignore.case = TRUE)
  loc      varname
1   1 Sepal.Length
2   3 Petal.Length

A slight modification to search_var() function can display matched variables if exist:

# Function to search variables and display if exist
show_var <- function(df, pattern, ...) {
loc <- grep(pattern, names(df), ...)
if (length(loc) == 0) warning("There are no such variables")
else return(df[loc])
}
iris %>%
tibble() %>%
show_var("length", ignore.case = TRUE)
# A tibble: 150 x 2
Sepal.Length Petal.Length
<dbl>        <dbl>
1          5.1          1.4
2          4.9          1.4
3          4.7          1.3
4          4.6          1.5
5          5            1.4
6          5.4          1.7
7          4.6          1.4
8          5            1.5
9          4.4          1.4
10          4.9          1.5
# ... with 140 more rows
• If the pattern was not found, you will get a warning: There are no such variables

## Dealing with zip files

• Suppose you want to read data in a zip file from the internet (say this file). We need to download the zip file from an URL, store it as a temporary file, unzip and read:
# Requires tidyverse & magrittr packages
library(tidyverse)
library(magrittr)

read_csv()