[R] Apply Function to Columns

Sarah sarahschmid98 at gmail.com
Fri Oct 24 09:24:15 CEST 2014


> Le 24 oct. 2014 à 09:23, Sarah <sarahschmid98 at gmail.com> a écrit :
> 
> 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 <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