[R] Changing a for loop to a function using sapply
Rui Barradas
ruipbarradas at sapo.pt
Sun Oct 21 21:50:31 CEST 2012
Hello,
You're right an _explicit_ for loop is not necessary in your case, but
note that the *apply functions are just for loops in disguise. They are
also the prefered R idiom.
In this case I've used ?replicate, a function of the *apply family. To
be more readable it uses a new function, called iteratively. I believe
that the end result looks simpler than the explicit loop version, but
that's a matter of taste.
# Changed from character vector to function
random.string <- function(n)
rep(NA, rs <- sample(3:n, 1, replace = TRUE))
# Unchanged
randomizeString <- function(x) {
DNAnucleotides<- c("a","c","g","t")
a <- sample(DNAnucleotides, 3, replace = TRUE)
return(a)
}
# Changed to use function random.string()
DNA <- character(20)
for(i in 1:20)
DNA[i] <- paste(unlist(sapply(random.string(21), randomizeString,
simplify = TRUE)), collapse = "")
DNA
# New function. Note the use of lapply, not sapply
fun <- function(n){
paste(unlist(lapply(random.string(n), randomizeString)), collapse = "")
}
DNA2 <- replicate(20, fun(21))
DNA2
Hope this helps,
Rui Barradas
Em 21-10-2012 16:14, Ed escreveu:
> Apparently there is one or more concepts that I do not fully
> understand from the descriptions of a function and the apply
> material. I have been reading the mail from this forum and have
> learned much but, in this case, what I have been reading here and from
> the manual isn't enough.
> The following code produces what I want with the for loop. From what
> I have read from this forum, a for loop its not necessarily the best
> path so I tried to create a function do to the same work.
>
> Using the following 64 bit version on Windows 7 Dell laptop
> R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
> Copyright (C) 2012 The R Foundation for Statistical Computing
> ISBN 3-900051-07-0
> Platform: x86_64-pc-mingw32/x64 (64-bit)
>
>
>
> below is the part that works
> ################################################################################
>
> # The following lines create a string of nucleotides and uses a for
> loop to create multiple strings.
> # random.string replicate something based on rs sampling criteria.
>
> random.string <- rep(NA, rs<-sample(3:18,1,replace = TRUE))
>
> # The randomizeString function uses members of DNAnucleotides list to
> sample 3 at a time
> # placing the results in "a".
>
> randomizeString <- function(x) {
> DNAnucleotides<- c("a","c","g","t")
> a <-sample(DNAnucleotides,3, replace = TRUE)
> return(a)
> }
>
> # The following paste output uses random.string to indicate the number
> of times the function
> # randomizeString selects a triplet from the list DNAnucleotides to
> create a text string
> # of a sequence of nucleotides.
> # collapse = "" removes the quotes from the triplets to produce one
> long string when the string
> # is printed by paste.
>
> paste(c(sapply(random.string, randomizeString, simplify = TRUE), ""),
> collapse = "")
>
> # The for loop uses the paste output to create multiple random length
> nucleotide strings
> # which can be printed to a file.
>
> for(i in 1:20) DNA[i]<-paste(c(sapply(rep(NA,
> rs<-sample(3:21,1,replace = TRUE))
> , randomizeString, simplify = TRUE), ""), collapse = "")
> DNA
>
> Rowname <- c(1:20) # provides row numbers to be used with the
> sequences produced
> Arrow<- rep(">",20) # provides a list of ">" to be used to
> separate the row numbers and sequences
>
> # DNAout uses a for loop to combine the vectors to create one string
> vector of sequences.
>
> DNAout<-class(character)
> for(j in 1:20)DNAout[j]<- paste(Rowname[j]," ",Arrow[j]," ",DNA[j],
> collapse = "" )
> DNAout
>
> ################################################################################################
>
>
> Here is what I have tried in attempts to create a function to
> replicate the results of the for loop above.
> This one comes close.
>
> #This repeats the above script without the comments.
> ######
> options(stringsAsFactors=FALSE)
> DNA<-class(character)
> randomizeString <- function(x) {
> DNAnucleotides <- c("A","C","G","T")
> a <-sample(DNAnucleotides, 3, replace = TRUE)
> return(a)
> }
> for(i in 1:20) DNA[i]<-paste(c(sapply(rep(NA,
> rs<-sample(3:18,1,replace = TRUE))
> , randomizeString, simplify = TRUE), ""), collapse = "")
> DNA
> Rowname <- c(1:20)
> Arrow<- rep(">",20)
>
> DNAout<-class(character)
> for(j in 1:20)DNAout[j]<- paste(Rowname[j]," ",Arrow[j]," ",DNA[j],
> collapse = "" )
> DNAout
> #######
> ##The following works partially
> DNAoutc <- class("character")
> DNAoutc <- function(x,y,z){sapply(x, paste(x," ",y," ",z,"\n",
> collapse = ""))}
> DNAoutc(Rowname,Arrow,DNA)
>
> Error in get(as.character(FUN), mode = "function", envir = envir) :
> object '1 > ACAAACAATGAGGTCCGCCGGATGAAGCTG
> 2 > CAAACCTCGTGCAAAGGTGCTTCATGGTAAATCCGTTTAGCTTTTCGGGAAAGT
> 3 > TACATCGAAGCTCGTGGGGTGAAG
> 4 > CGTCAACATGAACAAATGACATCCAGACGCACGCTGTAA
> 5 > CATTTAACCCTTGGTGTGATG
> 6 > AAGTATGAGTGGGCCTTGGGTTCTGGCTCCCACGCGTTGTGC
> 7 > AGTTCCCGCAAACTGATACTGATCAGCACTTAGAGACCGCCACTATCAGTT
> 8 > AATAATGCATGCTAGGCAGCCCGCTCGACCATTAGGGATAGAGCT
> 9 > GACATCAAGTCATAGGTT
> 10 > CAGAACAATATACACGTT
> 11 > CGCAACCATCTACACTGCGTT
> 12 > GTGAACTGAGGTATGACCAAAAGGTGGATAATACCCCACGGGACC
> 13 > TAGCAACATGAGTGCCTCAGGTTGTCGTTCAATAAACTCGGGAAG
> 14 > GCGATGATCCGCTTATAGCATGGACAAAGCAACGTTCTGTCGTCGGATTCGGGG
> 15 > AGCATGTTAGCAACCCCTTTG
> 16 > ACTAGTTCTGCCGTCATTTCAATG
> 17 > ATTCTTCCCTTG
> 18 > CATCTCGATTCTTTCTTACAATGT
> 19 > ATAGATACCTTGGTCAAATAATCGTTTCAAGGT
> 20 > GGGGTGGATAATAGCGGATAC
> ' of mode 'function' was not found
> ########################################
> My other attempts essentially give errors which I can not seem to
> figure out what I am missing to correct the errors.
> Below are a few of the failed attempts.
> #####################################
>
> mode(DNAoutf)<-("function")
> DNAoutf <- sapply(x,function(x,y,z){paste(x," ",y," ",z,"\n",
> collapse = "" )})
> DNAoutf(Rowname,Arrow,DNA)
>
> > mode(DNAoutf)<-("function")
> Error in mode(DNAoutf) <- ("function") : object 'DNAoutf' not found
> > DNAoutf<- function(x,y,z) {sapply(x,y,z),paste(x," ",y," ",z,"\n",
> collapse = "" ))}
> Error: unexpected ',' in "DNAoutf<- function(x,y,z) {sapply(x,y,z),"
> > DNAoutf(Rowname,Arrow,DNA)
> Error: could not find function "DNAoutf"
>
> #######################################
> mode(DNAoutf)<-("function")
> DNAoutf <- sapply(x,function(x,y,z){paste(x," ",y," ",z,"\n",
> collapse = "" )})
> DNAoutf(Rowname,Arrow,DNA)
>
> > mode(DNAoutf)<-("function")
> Error in mode(DNAoutf) <- ("function") : object 'DNAoutf' not found
> > DNAoutf <- sapply(x,function(x,y,z){paste(x," ",y," ",z,"\n",
> collapse = "" )})
> Error in paste(x, " ", y, " ", z, "\n", collapse = "") :
> argument "y" is missing, with no default
> > DNAoutf(Rowname,Arrow,DNA)
> Error: could not find function "DNAoutf"
>
> ############################################
> DNAouts(Rowname,Arrow,DNA) <- sapply(x,function(x,y,z){paste(x," ",y,"
> ",z,"\n", collapse = "" )})
> DNAouts
>
> > DNAouts(Rowname,Arrow,DNA) <- sapply(x,function(x,y,z){paste(x,"
> ",y," ",z,"\n", collapse = "" )})
> Error in paste(x, " ", y, " ", z, "\n", collapse = "") :
> argument "y" is missing, with no default
>
> ##############################################
> DNAouts <- class("character")
> DNAouts <- sapply(x,function(Rowname,Arrow,DNA){paste(x," ",y,"
> ",z,"\n", collapse = "",simplify, USE.NAMES)})
> DNAouts
>
> > DNAouts
> Error: object 'DNAouts' not found
> > DNAouts <- class("character")
> > DNAouts <- sapply(x,function(Rowname,Arrow,DNA){paste(x," ",y,"
> ",z,"\n", collapse = "",simplify, USE.NAMES)})
> Error in paste(x, " ", y, " ", z, "\n", collapse = "", simplify,
> USE.NAMES) :
> object 'z' not found
> > DNAouts
> [1] "character"
> >
> #######################################################
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list