library(libr)
# Create small sample dataframe
dat <- mtcars[1:10, c("mpg", "cyl")]
# Perform datastep and assign attributes
dat1 <- datastep(dat,
attrib = list(mpg = dsattr(label = "Miles Per Gallon"),
cyl = dsattr(label = "Cylinders"),
mpgcat = dsattr(label = "Fuel Efficiency")),
{
if (mpg >= 20)
mpgcat = "High"
else
mpgcat = "Low"
})
# Print results
dat1
# mpg cyl mpgcat
# Mazda RX4 21.0 6 High
# Mazda RX4 Wag 21.0 6 High
# Datsun 710 22.8 4 High
# Hornet 4 Drive 21.4 6 High
# Hornet Sportabout 18.7 8 Low
# Valiant 18.1 6 Low
# Duster 360 14.3 8 Low
# Merc 240D 24.4 4 High
# Merc 230 22.8 4 High
# Merc 280 19.2 6 Low
# Examine label attributes
attr(dat1$mpg, "label")
# [1] "Miles Per Gallon"
attr(dat1$cyl, "label")
# [1] "Cylinders"
attr(dat1$mpgcat, "label")
# [1] "Fuel Efficiency"
# See labels in viewer
# View(dat1)
Run the code above in your browser using DataLab