[R] Help with dlply, loop and column names
Igor Sosa Mayor
joseleopoldo1792 at gmail.com
Sun Mar 18 20:59:34 CET 2012
Hi,
I have a dataframe basically like this:
> head(asturias.gen2011[,c(1,4,9:14)])
municipio total upyd psoe pp iu fac tipo
440 Allande 2031 1.44 31.10 39.75 4.01 21.62 1000-10000
443 Aller 12582 1.37 33.30 37.09 15.53 10.35 10000-50000
567 Amieva 805 1.48 32.69 37.36 6.15 20.16 <1000
849 Avilés 84202 4.15 30.26 35.49 14.37 11.80 >50000
1087 Belmonte de Miranda 1751 1.66 38.42 35.74 7.22 14.81 1000-10000
1260 Bimenes 1894 0.98 34.28 26.87 23.30 10.98 1000-10000
I want to do the following:
1. for every party (psoe, pp, etc.) I want to create a variable like
this: upyd.lm.tipos, psoe.lm.tipos, etc.
2. I want to store in this variable a regression (psoe~total), but
split up by tipo.
I have the main idea of using dlply from the plyr vignette. But when I
try to put all this in a loop I'm coming into trouble and I'm at the
moment really confused how to solve this problem:
I have the following function:
elecregtipos <- function(y){
z<-dlply(asturias.gen2011, .(tipo), function(x) lm(x[,y]~x$edad.media))
# rsq<-function(x) summary(x)$r.squared
# bcoefs<-ldply(z, function(x) c(coef(x), rsquare=rsq(x)))
# return (bcoefs)
return(z)
}
And I try to call it with:
for (y in c("upyd", "psoe", "pp", "fac", "iu")) {
eval(parse(text=paste(y,'.lm.tipos', '<- elecregtipos(',y,')',sep='')))
}
At the moment I'm getting the error:
Error en `[.data.frame`(x, , y) : objeto 'upyd' no encontrado
If I call simply:
elecregtipos("upyd")
it works perfectly. The problem is the loop, column names, etc., but I'm
really confused what I still could try, because I have already tried any
possibility.
Any hint?
Thanks in advance.
--
:: Igor Sosa Mayor :: joseleopoldo1792 at gmail.com ::
:: GnuPG: 0x1C1E2890 :: http://www.gnupg.org/ ::
:: jabberid: rogorido :: ::
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 230 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120318/50acd6bc/attachment.bin>
More information about the R-help
mailing list