addCondition: Add a single column to existing data set based on a condition

Description Usage Arguments Value Examples

View source: R/addCondition.R

Description

Add a single column to existing data set based on a condition

Usage

1
addCondition(condDefs, dtOld, newvar)

Arguments

condDefs

Name of definitions for added column

dtOld

Name of data table that is to be updated

newvar

Name of new column to add

Value

An updated data.table that contains the added simulated data

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# New data set

def <- defData(varname = "x", dist = "categorical", formula = ".33;.33")
def <- defData(def, varname="y", dist="uniform", formula="-5;5")

dt <- genData(1000, def)

# Define conditions

defC <- defCondition(condition = "x == 1", formula = "5 + 2*y-.5*y^2",
                     variance = 1,dist = "normal")
defC <- defCondition(defC, condition = "x == 2",
                     formula = "3 - 3*y + y^2", variance = 2, dist="normal")
defC <- defCondition(defC, condition = "x == 3",
                     formula = "abs(y)", dist="poisson")

# Add column

dt <- addCondition(defC, dt, "NewVar")

# Plot data

library(ggplot2)

ggplot(data = dt, aes(x=y, y=NewVar, group = x)) +
  geom_point(aes(color = factor(x)))

kgoldfeld/simstudy documentation built on Nov. 8, 2018, 7:41 p.m.