[R] Apply Function to Columns

Rui Barradas ruipbarradas at sapo.pt
Thu Oct 23 17:49:38 CEST 2014


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 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