Learn R Programming

expss (version 0.11.6)

add_columns: Add columns to data.frame.

Description

add_columns inspired by MATCH FILES (Add variables...) from SPSS Statistics. It works similar to SQL left join but number of cases in the left part always remain the same. If there are duplicated keys in the y then error will be raised by default.

Usage

add_columns(x, y, by = NULL, ignore_duplicates = FALSE, ...)

Value

data.frame

Arguments

x

data.frame to be joined with y.

y

data.frame.

by

character vector or NULL(default) or 1. Names of common variables in the x and y by which we will attach y to x. If it is NULL then common names will be used. If it is equals to 1 then we will use the first column from both dataframes. To add columns by different variables on x and y use a named vector. For example, by = c("a" = "b") will match x.a to y.b.

ignore_duplicates

logical Should we ignore duplicates in the by variables in the y? If it is TRUE than first occurrence of duplicated key will be used.

...

arguments for further methods

Examples

Run this code

# example for 'add_columns' from base 'merge'
authors = data.frame(
    surname = c("Tukey", "Venables", "Tierney", "Ripley", "McNeil"),
    nationality = c("US", "Australia", "US", "UK", "Australia"),
    deceased = c("yes", rep("no", 4))
)

books = data.frame(
    surname = c("Tukey", "Venables", "Tierney",
                "Ripley", "Ripley", "McNeil", "R Core"),
    title = c("Exploratory Data Analysis",
              "Modern Applied Statistics ...",
              "LISP-STAT",
              "Spatial Statistics", "Stochastic Simulation",
              "Interactive Data Analysis",
              "An Introduction to R")
)

add_columns(books, authors)

Run the code above in your browser using DataLab