You are here:
Vanderbilt Biostatistics Wiki
>
Main Web
>
TWikiUsers
>
ColeBeck
>
ColeWritesNR
(12 Dec 2007,
ColeBeck
)
(raw view)
E
dit
A
ttach
Create a sample dataset - *values* arguments takes *alist* <highlight> # 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)) </highlight> For all the people out there who want to create Perl hashes out of their R lists: <highlight> # 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) } } </highlight> If you'd like to check the validity of an email address, use this function: <highlight> validemail<-function(address) length(grep("^[\\w!#\\$%&'*+\\-\\/=?^`{|}~][\\w!#\\$%&'*+\\-\\/=?^`{|}~.]{0,62}[\\w!#\\$%&'*+\\-\\/=?^`{|}~]{1}@([\\w\\d\\-]+\\.)+[\\w\\d\\-]+$",address,perl=TRUE)) </highlight>
E
dit
|
A
ttach
|
P
rint version
|
H
istory
: r3
<
r2
<
r1
|
B
acklinks
|
V
iew topic
|
Edit
w
iki text
|
M
ore topic actions
Topic revision: r3 - 12 Dec 2007,
ColeBeck
Main
Department Home Page
Biostatistics Graduate Program
Vanderbilt University Medical Center
Main Web
Main Web Home
Search
Recent Changes
Changes
Topic list
Biostatistics Webs
Archive
Main
Sandbox
System
Register
|
Log In
Copyright © 2013-2022 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