Learn R Programming

questionr (version 0.2)

lookfor: Look for keywords variable names and descriptions

Description

lookfor emulates the lookfor Stata command in R. It supports searching into the variable names of regular R data frames as well as into SPSS and Stata datasets loaded in R via the foreign or memisc packages, in which case it will also search variable descriptions (labels). The command is meant to help users finding variables in large datasets.

Usage

lookfor(data, keywords = "weight|sample", labels = TRUE,
    ignore.case = TRUE)

Arguments

data
a data frame that can be annotated by the foreign or memisc packages.
keywords
a character string, which can be formatted as a regular expression suitable for a grep pattern, or a vector of keywords; searches for keywords 'sample' and 'weight' by default
labels
whether or not to search variable labels (descriptions); TRUE by default
ignore.case
whether or not to make the keywords case sensitive; TRUE by default (case is ignored during matching)

Value

  • a data frame featuring the variable position, name and description (if it exists) in the original data frame

source

Based on the behaviour of the lookfor command in Stata. Future versions might include fuzzey search as featured by the query function of the memisc package, which also searches value labels and therefore offers a wider search scope.

Details

The function looks into the variable names for matches to the keywords. If the data frame has been read into R with a method that provides variable labels (read.dta or read.spss in the foreign package, or any of the importer methods of the memisc package), then variable labels are included in the search scope.

See Also

query in the memisc package

Examples

Run this code
# Look for a single keyword.
lookfor(iris, "petal")
# Load memisc package and example data.
require(memisc)
nes1948.por <- UnZip("anes/NES1948.ZIP","NES1948.POR", package="memisc")
nes1948 <- spss.portable.file(nes1948.por)
# Look for a vector of keywords.
lookfor(nes1948, c("Truman", "Dewey"))
# Look for a regular expression.
lookfor(nes1948, "truman|dewey")
# Look for a phrase.
lookfor(nes1948, "personal attribute")

Run the code above in your browser using DataLab