[R] RE : Re: Apply Function to Columns

Sarah Schmid sarahschmid98 at gmail.com
Fri Oct 24 14:45:56 CEST 2014


Hello ! 

Yes, I realized just after sending you the email that I didn't cc the list, so I sent it again just after that. 

And thank you very much for your help, it works perfectly ! 

And the package I used was ncf.

Sarah

-------- Message d'origine --------
De : Rui Barradas <ruipbarradas at sapo.pt> 
Date :  
A : Sarah <sarahschmid98 at gmail.com> 
Cc : R-help at r-project.org 
Objet : Re: [R] Apply Function to Columns 
 
Hello,

Please cc the list, the odds of getting more and better answers are greater.
And you should tell us from what package do the function plot.correlog 
comes. library(what)?

As for your question, assuming you want to save your plots as PNG files, 
you could do something like the following.


fun <- function(p){
fname <- paste0("correlog", p)
png(filename = fname)
plot.correlog(...)
dev.off()
}
ddeg.correlog.list <- lapply(9:20, fun)

See the help page for ?png. It lists several other graphics file formats 
you can use.

Hope this helps,

Rui Barradas

Em 24-10-2014 08:23, Sarah escreveu:
> Thank you very much, it helped a lot!
>
> I just have another question know. I want to make plot for every
> species. I just add the function « plot correlog » to the previous
> function and I have now the following script:
>
> ddeg.correlog.list <- lapply(9:20, function(p)
> *plot.correlog*(correlog(plant[plant[,p]=="1", 2], plant[plant[,p]=="1",
> 3],
>                           plant[plant[,p]=="1", 4],increment=2500)))
>
> It’s working, but I wanted to know if there is a way to save each plot.
> I found that I can use the dev.off function or the ggplot function of
> the package ggplot2, but I can’t figure out how to use it within the
> lapply function. Do you think there is a way?
>
> Thank you for your help!
>
> Sarah
>
>> Le 23 oct. 2014 à 17:49, Rui Barradas <ruipbarradas at sapo.pt
>> <mailto:ruipbarradas at sapo.pt>> a écrit :
>>
>> Hello,
>>
>> Yes, you can use lapply. Maybe something like the following. Note that
>> the result is a list with one member per species. (Untested).
>>
>> ddeg.correlog.list <- lapply(9:11, function(p)
>> correlog(plant[plant[,p]=="1", 2], plant[plant[,p]=="1", 3],
>> plant[plant[,p]=="1", 4]))
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Em 23-10-2014 16:06, Sarah escreveu:
>>> Hello List,
>>>
>>> I have a database which consist of 912 plots. For each plot, I have
>>> the presence/absence information of 260 species of plants and also 5
>>> different environmental variables (ddeg, mind, srad, slp, topo).
>>>
>>> The dataframe looks like this:
>>>
>>>   Plot_Number        X Y ddeg mind   srad slp topo Galium_mollugo
>>> Gentiana_nivalis
>>> 1        1 557747.6 149726.8 2598 -625 236363   8  176              0
>>>                0
>>> 2        2 572499.4 145503.5 2178 -176 161970  14 -137              0
>>>                0
>>> 3        3 579100.4 151800.4 1208  632 267572  33  129              0
>>>                0
>>> 4        4 581301.7 150300.1 1645   83 246633  15  -70              0
>>>                0
>>> 5        5 579838.7 124770.9 1102 1637 158300   2 -231              0
>>>                0
>>> 6        6 577011.1 121328.6  731 2223 180286  41   70              0
>>>                0
>>>
>>> Now, what I wanted to do is to calculate spatial autocorrelation of
>>> each environmental variable for each species, but only for the plots
>>> where the species is present.
>>>
>>> I will use the correlog function of the package ncf (doesn’t really
>>> matter). The correlog function work with an argument X which is the
>>> longitude, an argument Y which is the latitude and an argument Z
>>> which is the variable you want to test for autocorrelation (in my
>>> case, the different environmental variables).
>>>
>>> So, for the first species I have the following script:
>>>
>>> ddeg.correlog.9<-correlog(plant[plant[,9]=="1", 2],
>>> plant[plant[,9]=="1", 3], plant[plant[,9]=="1", 4])
>>>
>>> X = plant[plant[,9]=="1", 2] —>  only the X coordinate where my
>>> species 9 is present
>>> Y = plant[plant[,9]=="1", 3] —>  only the Y coordinate where my
>>> species 9 is present
>>> Z = plant[plant[,9]=="1", 4] —>  only the value of the environmental
>>> variable where my species 9 is present
>>>
>>> plant: dataframe
>>> 9: column corresponding to the first species
>>> 2: column corresponding to the X coordinate
>>> 3: column correspondind to the Y coordinate
>>> 4: column corresponding to the first environmental variable
>>>
>>> So my question is: how do I repeat this script for every species
>>> (basically, I just have to change the number « 9 » into 10, 11 and so
>>> on) ?
>>>
>>> I try to write a function but I’m new in R and didn’t manage to do
>>> it. I was also considering to use the function « lapply », but I
>>> don’t think I can use it in this case, isn’t it?
>>>
>>> Thank you very much for your help !
>>>
>>> Sarah
>>> ______________________________________________
>>> R-help at r-project.org <mailto: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.
>>>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list