questionr (version 0.7.0)

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 haven, 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, ..., labels = TRUE, ignore.case = TRUE,
  details = FALSE)

Arguments

data

a data frame

...

list of keywords, a character string (or several character strings), which can be formatted as a regular expression suitable for a grep pattern, or a vector of keywords; displays all variables if not specified

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)

details

add details about each variable (see examples)

Value

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

Details

The function looks into the variable names for matches to the keywords. If the data frame has been imported into R with haven package, then variable labels are included in the search scope. If labelled package is installed, variable labels of data.frame imported with foreign or memisc packages will also be taken into account.

See Also

query in the memisc package

Examples

Run this code
# NOT RUN {
lookfor(iris)
# Look for a single keyword.
lookfor(iris, "petal")
lookfor(iris, "s")
# Look for with a regular expression
lookfor(iris, "petal|species")
lookfor(iris, "s$")
# Look for with several keywords
lookfor(iris, "pet", "sp")
lookfor(iris, "pet", "sp", "width")
# Load memisc package and example data.
# }
# NOT RUN {
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")
# }
# NOT RUN {
# Labelled data
data(fecondite)
lookfor(femmes)
lookfor(femmes, "date")
# Display details
lookfor(femmes, details = TRUE)
# }

Run the code above in your browser using DataCamp Workspace