[R] Loop Help

David Winsemius dwinsemius at comcast.net
Mon Feb 29 18:03:12 CET 2016


> On Feb 29, 2016, at 6:24 AM, Fernando McRayearth <urayearth at gmail.com> wrote:
> 
> Need to create ascii maps for 10 species by writing a loop. So i have to have the vectors ready in the Global Environment, and the "raster map" so the information can be added. 
> 
> when writing the loop I am using the "paste" function because the only thing that changes in the vector is the name of the specie ( ens_KAPPA_F45_Ambystoma.altamirani, ens_KAPPA_F45_Ambystoma.lermaense), now the issue is that the result returns it like this "Ambystoma.lermaense" , and i don't know how to bring the vector without the "". any suggestions? thanks. 
> 
> 
> CURRENT CODE NOT WORKING 
> for (i in 1:length(amph_nome)){
> orden=match(testo,0:93831)
> amf1=paste("ens_KAPPA_F45_",amph_nome[i],sep="")

You are trying to use R as a macro language, a common source of difficulty for persons coming from other language backgrounds. If you have committed to this course (instead of using R's capacities to handle lists) then you will nee to use the `get` function which returns a data-objent when given a character vector name.

working <- get(amf1)

> 
> #amf3=amf2[orden]

# Perhaps (only guessing since you do not describe the algorithm desired
# and `Map` is not defined in your code and on my machine `Map` is a "funprog"-function

amf0 <- working[ orden ]


> r1=Map
> values(r1)=amf2
> }
> 
> 
> IF I WAS TO DO IT ONE BY ONE THIS VERSION WORKS 
> 
> orden=match(testo,0:93831)
> amf0=ens_KAPPA_F45_Ambystoma.altamirani[orden]
> r1=Map
> values(r1)=amf0
> 
> writeRaster(r1, filename = "r1.asc",dataType=ascii,overwrite=TRUE)
> 
> Sent from Mail for Windows 10
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list