# construct data frame
mydata <- read.table(text="Status Severity
1 Mild
4 Moderate
3 Moderate
2 Mild
1 Severe", header=TRUE)
# replace Status with a transformed version
Transform(Status=Status-1)
# abbreviated form
# replace original with recoded
trans(StatusNew=Status-1)
# replace Status with a transformed version
# leave input mydata unmodified
# save transformed data frame to the created data frame called newdata
newdata <- Transform(Status=Status-1, keep=FALSE)
# construct data frame
# recode Status into a factor
Transform(Status=factor(Status, labels=c("OK","Hurts","Painful","Yikes")))
# read lessR data set datEmployee into data frame mydata
Read(lessR.data="Employee")
# multiple transformations in one statement
# Months is a new variable
# Salary is a new version of the old Salary
# Satisfaction was read as non-numeric, so as a factor, but now is ordinal
# HealthPlan was read as numeric values 0,1,2, now converted to a factor
Transform(
Months=Years*12,
Salary=Salary/1000,
Satisfaction=factor(Satisfaction, levels=c("low", "med", "high"), ordered=TRUE),
HealthPlan=factor(HealthPlan,
levels=c(0,1,2), labels=c("GoodHealth", "YellowCross", "BestCare"))
)
Run the code above in your browser using DataLab