### start script ###
# Dummy variable function
datdum <- function(x, data, name){
data$rv <- rnorm(dim(data)[1],1,1)
mm <- data.frame(model.matrix(lm(data$rv~-1+factor(data[,x]))))
names(mm) <- paste(name)
data$rv <- NULL
data <- cbind(data,mm)
return(data)
}
# Creating a simple example dataframe "dat" with one column "dat1" consisting of the values "A","B","C".
dat1 <- c("A","B","C")
dat <- data.frame(dat1)
# Using the dummy variable function:
datdum(x="dat1",data=dat, name=dat1)
### end script ###
Maybe it will save you time too.
Cheers Toke
Ingen kommentarer:
Send en kommentar