Dear R users, I have two data frames that consist of statistical information for most countries around the world. One dataframe consists of the latitude and longitude ("coord.csv") of each country, while the other consists of 100's of different attributes ("countryattri.csv") for each country (like, GDP, Population, etc.). The data is organized with a header and then countries down the first column. I'm trying to run a spatial autocorrelation for each column or attribute in the "attri" dataframe. The process for running this on a single column is as follows: example of "coord.csv" dataframe: country, lat, long Albania, 41.00, 20.00 Algeria, 28.00, 3.00 Angola, -12.30, 18.30 example of "countryattri.csv" dataframe: country, GDP, Population Albania, 100000, 20000 Algeria, 1200000, 300000 Angola, 1300000, 300000> Locate<-read.csv("coord.csv", header = TRUE, sep = ",") > Locate.dists<-as.matrix(dist(cbind(Locate$long, Locate$lat))) > Locate.dists.inv<-1/((Locate.dists)^2) > diag(Locate.dists.inv)<-0 > attri<-read.csv("countryattri.csv", header = TRUE, sep = ",") > Moran.I(attri$GDP, Locate.dists.inv, na.rm = TRUE)This gives you Moran's I (correlation coefficient) for the GDP column in the "attri" dataframe. I am trying to run the Moran.I function as a loop for all columns in the "attri" dataframe so that I can obtain Moran coefficients for every attribute. I've tried a number of different approaches but cannot get a column loop to run through this function. I would be very grateful if someone could help me with this problem. Thank you, *Scott Seely* p.s. the Moran.I function is a part of the "ape" package. [[alternative HTML version deleted]]
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Aug-05 00:36 UTC
[R] Running a column loop through the Moran.I function.
I'm on my phone so I can't verify this but have you tried the apply function? apply(attri,2,Moran.I,Locate.dists.inv,NA.rm=T) Michael Weylandt On Aug 4, 2011, at 4:54 PM, Scott Seely <sseely at thetus.com> wrote:> Dear R users, > > I have two data frames that consist of statistical information for most > countries around the world. One dataframe consists of the latitude and > longitude ("coord.csv") of each country, while the other consists of 100's > of different attributes ("countryattri.csv") for each country (like, GDP, > Population, etc.). The data is organized with a header and then countries > down the first column. I'm trying to run a spatial autocorrelation for each > column or attribute in the "attri" dataframe. The process for running this > on a single column is as follows: > > example of "coord.csv" dataframe: > > country, lat, long > Albania, 41.00, 20.00 > Algeria, 28.00, 3.00 > Angola, -12.30, 18.30 > > example of "countryattri.csv" dataframe: > > country, GDP, Population > Albania, 100000, 20000 > Algeria, 1200000, 300000 > Angola, 1300000, 300000 > >> Locate<-read.csv("coord.csv", header = TRUE, sep = ",") >> Locate.dists<-as.matrix(dist(cbind(Locate$long, Locate$lat))) >> Locate.dists.inv<-1/((Locate.dists)^2) >> diag(Locate.dists.inv)<-0 >> attri<-read.csv("countryattri.csv", header = TRUE, sep = ",") >> Moran.I(attri$GDP, Locate.dists.inv, na.rm = TRUE) > > This gives you Moran's I (correlation coefficient) for the GDP column in the > "attri" dataframe. I am trying to run the Moran.I function as a loop for all > columns in the "attri" dataframe so that I can obtain Moran coefficients for > every attribute. > > I've tried a number of different approaches but cannot get a column loop to > run through this function. I would be very grateful if someone could help me > with this problem. > > Thank you, > > *Scott Seely* > p.s. the Moran.I function is a part of the "ape" package. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.