I have a question about changing rownames. In the following function I am plotting the regression coeficients with their corresponding mean. Right now, the labels on the plot and the rownames in the dataframe are x1,x2,x3...etc. Is there a way to make the row names the same as the variable name entered into the formula? I.E. if x1 was a vector called opinions, it would be labeled opinions on the plot instead of x1. I have tried using variations of rownames(x)<- namevector but have not been successful. I keep getting a null. Any suggestions would be appreciated Quadplot<-function(y, x1, x2, x3 = NULL, x4 NULL,x5=NULL,x6=NULL,x7=NULL,x8=NULL,x9=NULL,x10=NULL, x11=NULL, x12=NULL, x13=NULL, x14=NULL) #section of code ommitted my.formula <- as.formula("y ~ x1 + x2 + x3+ x4+ x5+ x6+ x7+ x8+ x9+x10+x11+x12+x13+x14") outlm <- lm(my.formula) #section of code ommitted meanvec<-c(mean(x1,na.rm=TRUE),mean(x2,na.rm=TRUE),mean(x3,na.rm=TRUE),mean(x4,na.rm=TRUE),mean(x5,na.rm=TRUE),mean(x6,na.rm=TRUE),mean(x7,na.rm=TRUE),mean(x8,na.rm=TRUE),mean(x9,na.rm=TRUE),mean(x10,na.rm=TRUE),mean(x11,na.rm=TRUE) ,mean(x12,na.rm=TRUE) ,mean(x13,na.rm=TRUE) ,mean(x14,na.rm=TRUE)) } meanper<-meanvec/6 meanper1<-meanper*100 #This is the data frame created mf <- data.frame(Impact = abs(outlm$coef[-1]), Performance = meanper1) meanx<-mean(abs(outlm$coef[-1])) meany<-mean(meanper1) n<-length(meanvec) color<-rep("blue",n) color[(mf$Impact>meanx & mf$Performance>meany)]<-"green" color[(mf$Impact>meanx & mf$Performance<meany)]<-"red" color[(mf$Impact<meanx & mf$Performance>meany)]<-"gray" plot((mf),col=color,main="Perfomance vs. Impact",xlab="Impact",ylab="Performance", type = "n") points(mf, pch=21, bg=color, cex=4) text(x = mf$Impact, y = mf$Performance, labels rownames(mf)) abline(h=mean(mf$Performance)) abline(v=mean(mf$Impact)) print(mf) } Thanks Derek
On 4 Jan 2005 at 10:25, Derek Margetts wrote:> > I have a question about changing rownames. In the > following function I am plotting the regression > coeficients with their corresponding mean. Right now, > the labels on the plot and the rownames in the > dataframe are x1,x2,x3...etc. Is there a way to make > the row names the same as the variable name entered > into the formula? I.E. if x1 was a vector called > opinions, it would be labeled opinions on the plot > instead of x1. I have tried using variations of > rownames(x)<- namevector but have not been successful.If your mf is your data frame rownames(mf) <- namevector with the same length should work. BTW, If you have x1-x14 in data frame e.g. yourdf you can use meanvec<-colMeans(yourdf, na.rm=TRUE) Cheers Petr> I keep getting a null. Any suggestions would be > appreciated > > Quadplot<-function(y, x1, x2, x3 = NULL, x4 > NULL,x5=NULL,x6=NULL,x7=NULL,x8=NULL,x9=NULL,x10=NULL, > x11=NULL, x12=NULL, x13=NULL, x14=NULL) > > #section of code ommitted > > my.formula <- as.formula("y ~ x1 + x2 + x3+ x4+ x5+ > x6+ x7+ x8+ x9+x10+x11+x12+x13+x14") > outlm <- lm(my.formula) > > #section of code ommitted > > meanvec<-c(mean(x1,na.rm=TRUE),mean(x2,na.rm=TRUE),mean(x3,na.rm=TRUE) > ,mean(x4,na.rm=TRUE),mean(x5,na.rm=TRUE),mean(x6,na.rm=TRUE),mean(x7,n > a.rm=TRUE),mean(x8,na.rm=TRUE),mean(x9,na.rm=TRUE),mean(x10,na.rm=TRUE > ),mean(x11,na.rm=TRUE) ,mean(x12,na.rm=TRUE) ,mean(x13,na.rm=TRUE) > ,mean(x14,na.rm=TRUE)) } meanper<-meanvec/6 meanper1<-meanper*100 > > #This is the data frame created > mf <- data.frame(Impact = abs(outlm$coef[-1]), > Performance = meanper1) > > meanx<-mean(abs(outlm$coef[-1])) > meany<-mean(meanper1) > n<-length(meanvec) > color<-rep("blue",n) > color[(mf$Impact>meanx & > mf$Performance>meany)]<-"green" > color[(mf$Impact>meanx & mf$Performance<meany)]<-"red" > color[(mf$Impact<meanx & > mf$Performance>meany)]<-"gray" > > plot((mf),col=color,main="Perfomance vs. > Impact",xlab="Impact",ylab="Performance", type = "n") > points(mf, pch=21, bg=color, cex=4) > text(x = mf$Impact, y = mf$Performance, labels > rownames(mf)) > abline(h=mean(mf$Performance)) > abline(v=mean(mf$Impact)) > print(mf) > } > > Thanks > Derek > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz
It is sometimes important to know that row.names accesses the row names of a data frame, and rownames the first dimname of an array (including a matrix). They are almost equivalent, but not quite (e.g. row.names is generic and has methods for matrices and arrays). So the example would optimally be row.names(mf) <- namevector This tests for the validity of the row names. On Wed, 5 Jan 2005, Petr Pikal wrote:> On 4 Jan 2005 at 10:25, Derek Margetts wrote: >> >> I have a question about changing rownames. In the >> following function I am plotting the regression >> coeficients with their corresponding mean. Right now, >> the labels on the plot and the rownames in the >> dataframe are x1,x2,x3...etc. Is there a way to make >> the row names the same as the variable name entered >> into the formula? I.E. if x1 was a vector called >> opinions, it would be labeled opinions on the plot >> instead of x1. I have tried using variations of >> rownames(x)<- namevector but have not been successful. > > If your mf is your data frame > > rownames(mf) <- namevector > > with the same length should work. > > BTW, If you have x1-x14 in data frame e.g. yourdf you can use > > meanvec<-colMeans(yourdf, na.rm=TRUE) > > Cheers > Petr > >> I keep getting a null. Any suggestions would be >> appreciated >> >> Quadplot<-function(y, x1, x2, x3 = NULL, x4 >> NULL,x5=NULL,x6=NULL,x7=NULL,x8=NULL,x9=NULL,x10=NULL, >> x11=NULL, x12=NULL, x13=NULL, x14=NULL) >> >> #section of code ommitted >> >> my.formula <- as.formula("y ~ x1 + x2 + x3+ x4+ x5+ >> x6+ x7+ x8+ x9+x10+x11+x12+x13+x14") >> outlm <- lm(my.formula) >> >> #section of code ommitted >> >> meanvec<-c(mean(x1,na.rm=TRUE),mean(x2,na.rm=TRUE),mean(x3,na.rm=TRUE) >> ,mean(x4,na.rm=TRUE),mean(x5,na.rm=TRUE),mean(x6,na.rm=TRUE),mean(x7,n >> a.rm=TRUE),mean(x8,na.rm=TRUE),mean(x9,na.rm=TRUE),mean(x10,na.rm=TRUE >> ),mean(x11,na.rm=TRUE) ,mean(x12,na.rm=TRUE) ,mean(x13,na.rm=TRUE) >> ,mean(x14,na.rm=TRUE)) } meanper<-meanvec/6 meanper1<-meanper*100 >> >> #This is the data frame created >> mf <- data.frame(Impact = abs(outlm$coef[-1]), >> Performance = meanper1) >> >> meanx<-mean(abs(outlm$coef[-1])) >> meany<-mean(meanper1) >> n<-length(meanvec) >> color<-rep("blue",n) >> color[(mf$Impact>meanx & >> mf$Performance>meany)]<-"green" >> color[(mf$Impact>meanx & mf$Performance<meany)]<-"red" >> color[(mf$Impact<meanx & >> mf$Performance>meany)]<-"gray" >> >> plot((mf),col=color,main="Perfomance vs. >> Impact",xlab="Impact",ylab="Performance", type = "n") >> points(mf, pch=21, bg=color, cex=4) >> text(x = mf$Impact, y = mf$Performance, labels >> rownames(mf)) >> abline(h=mean(mf$Performance)) >> abline(v=mean(mf$Impact)) >> print(mf) >> } >> >> Thanks >> Derek >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide! >> http://www.R-project.org/posting-guide.html > > Petr Pikal > petr.pikal at precheza.cz > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595