# Emulate R

## c()

Agate’s `Table.select()` and `Table.exclude()` are the equivalent of R’s `c` for selecting columns.

R:

    selected <- data[c("last_name", "first_name", "age")]
    excluded <- data[!c("last_name", "first_name", "age")]

agate:

    selected = table.select(['last_name', 'first_name', 'age'])
    excluded = table.exclude(['last_name', 'first_name', 'age'])

## subset

Agate’s `Table.where()` is the equivalent of R’s `subset`.

R:

    newdata <- subset(data, age >= 20 | age < 10)

agate:

    new_table = table.where(lambda row: row['age'] >= 20 or row['age'] < 10)

## order

Agate’s `Table.order_by()` is the equivalent of R’s `order`.

R:

    newdata <- employees[order(last_name),]

agate:

    new_table = employees.order_by('last_name')

## merge

Agate’s `Table.join()` is the equivalent of R’s `merge`.

R:

    joined <- merge(employees, states, by="usps")

agate:

    joined = employees.join(states, 'usps')

## rbind

Agate’s `Table.merge()` is the equivalent of R’s `rbind`.

R:

    merged <- rbind(first_year, second_year)

agate:

    merged = agate.Table.merge(first_year, second_year)

## aggregate

Agate’s `Table.group_by()` and `TableSet.aggregate()` can be used to recreate the functionality of R’s `aggregate`.

R:

    aggregates = aggregate(employees$salary, list(job = employees$job), mean)

agate:

    jobs = employees.group_by('job')
    aggregates = jobs.aggregate([
        ('mean', agate.Mean('salary'))
    ])

## melt

Agate’s `Table.normalize()` is the equivalent of R’s `melt`.

R:

    melt(employees, id=c("last_name", "first_name"))

agate:

    employees.normalize(['last_name', 'first_name'])

## cast

Agate’s `Table.denormalize()` is the equivalent of R’s `cast`.

R:

    melted = melt(employees, id=c("name"))
    casted = cast(melted, name~variable, mean)

agate:

    normalized = employees.normalize(['name'])
    denormalized = normalized.denormalize('name')