Create a sample dataset - values arguments takes alist

# function to create a sample dataset quickly
sample.df=function(rows=10,cols=10,names=NA,values=NA){
    lookup=list(names=character(cols), values=character(cols))
# lookup=list(names=character(cols), values=character(cols), stringsAsFactors=FALSE)
# get all the names
    if(length(names) > 1 || !is.na(names)) {
        for(i in 1:length(names))
            lookup$names[i]=names[i]
    }
    for(i in 1:cols) {
        if(lookup$names[i]=="") lookup$names[i]=i
    }
# get all the values
    if(length(values) > 1 || !is.na(values)) {
        for(i in 1:length(values))
            if(length(names(values))  && names(values)[i]!='')
                lookup$values[which(lookup$names==names(values)[i])]=values[i]
            else
                lookup$values[i]=values[i]
    }
    for(i in 1:cols) {
        if(lookup$values[i]=="") lookup$values[i]="1:10"
    }
    mydf='data.frame('
    for(i in 1:cols) {
        mydf<-paste(mydf,"'",lookup$names[i],"'=sample(",lookup$values[i],', size=',rows,', replace=TRUE), ',sep='')
    }
    eval(parse(text=paste(mydf, 'check.names=F)', sep='')))
}

sample.df(5,15,letters[1:15],alist('c'=c('M','F'),'o'=1:100))

For all the people out there who want to create Perl hashes out of their R lists:

# convert a R list into a Perl hash
list2hash<-function(data,firstcall=TRUE) {
    output<-''

    if(is.list(data)) {
        if(length(names(data)) > 0) {
            output<-"{\n"
            for(i in names(data)) {
                key<-i
#                 value<-data$i
                value<-eval(parse(text=paste("data",i,sep="$")))
                output<-paste(output,key," => ",list2hash(value,FALSE),"\n",sep='')
            }
            output<-paste(output,"},\n",sep='')
        } else {
#             make me a vector
            output<-"[\n"
            for(i in data) {
                output<-paste(output,list2hash(i,FALSE),"\n",sep="")
            }
            output<-paste(output,"],\n",sep='')
        }
    } else {
        if(is.vector(data) && length(data) > 1) {
#             I am a vector
            output<-"[\n"
            for(i in data) {
                output<-paste(output,list2hash(i,FALSE),"\n",sep="")
            }
            output<-paste(output,"],\n",sep='')
        } else {
#             I'm not a couth vector (where couth>1), nor a list - so I must be a single value
            output<-paste("'",data,"',",sep='')
        }
    }
    if(firstcall) {
#         if you wanted to prefix the output with '$VAR='
#         myoutput<-paste("$",substitute(data),"=",substr(output,0,nchar(output)-2),";\n",sep='')
        myoutput<-paste(substr(output,0,nchar(output)-2),";\n",sep='')
        return(cat(myoutput))
    } else {
        return(output)
    }
}

If you'd like to check the validity of an email address, use this function:

validemail<-function(address) length(grep("^[\\w!#\\$%&'*+\\-\\/=?^`{|}~][\\w!#\\$%&'*+\\-\\/=?^`{|}~.]{0,62}[\\w!#\\$%&'*+\\-\\/=?^`{|}~]{1}@([\\w\\d\\-]+\\.)+[\\w\\d\\-]+$",address,perl=TRUE))
Topic revision: r3 - 12 Dec 2007, ColeBeck
 

This site is powered by FoswikiCopyright © 2013 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback