Re: [R] Re : Adding column sum to new row in data frame

2010-05-20 Thread David Winsemius
On May 20, 2010, at 1:01 AM, Joshua Wiley wrote: Dear Mohan, First, I would like to modify my code slightly to: data <- rbind(data,data.frame(State="Total",t(apply(data[,-1], 2, sum, na.rm=TRUE This actually will add a 7th level to your factor automatically. The reason I wanted to chang

Re: [R] Re : Adding column sum to new row in data frame

2010-05-19 Thread Mohan L
On Thu, May 20, 2010 at 10:31 AM, Joshua Wiley wrote: > Dear Mohan, > > First, I would like to modify my code slightly to: > > data <- rbind(data,data.frame(State="Total",t(apply(data[,-1], 2, sum, > na.rm=TRUE > > This actually will add a 7th level to your factor automatically. The > reason

Re: [R] Re : Adding column sum to new row in data frame

2010-05-19 Thread Joshua Wiley
Dear Mohan, First, I would like to modify my code slightly to: data <- rbind(data,data.frame(State="Total",t(apply(data[,-1], 2, sum, na.rm=TRUE This actually will add a 7th level to your factor automatically. The reason I wanted to change from using c() to data.frame() is that if one uses

Re: [R] Re : Adding column sum to new row in data frame

2010-05-19 Thread David Winsemius
On May 19, 2010, at 11:19 PM, Mohan L wrote: How to safely avoid this warning massage? Now I have instead of "Total" in last row State column. How to I replace it as "Total"? Dear All, The below link provides a very good explanation of "Creating factor variables" and way to avoid the war

Re: [R] Re : Adding column sum to new row in data frame

2010-05-19 Thread Wu Gong
To remove NA instead of Total: rbind(data,cbind(State="Total", t(apply(data[,-1],2,sum - A R learner. -- View this message in context: http://r.789695.n4.nabble.com/Re-Adding-column-sum-to-new-row-in-data-frame-tp2223277p2223855.html Sent from the R help mailing list archive at Nabb

Re: [R] Re : Adding column sum to new row in data frame

2010-05-19 Thread Mohan L
> > > How to safely avoid this warning massage? > Now I have instead of "Total" in last row State column. How to I > replace it as "Total"? > > Dear All, The below link provides a very good explanation of "Creating factor variables" and way to avoid the warning message http://www.ats.ucla.edu/st

Re: [R] Re : Adding column sum to new row in data frame

2010-05-19 Thread Mohan L
Hi Joshua, > > > > rbind(data, c("Total",apply(data[,-1], 2, sum, na.rm=TRUE))) > Yes. This what exactly I want. Thanks for your time. > > If your State column is a factor, it will return a warning that NAs > were introduced (but the totals will still be at the bottom). > Yes. > is.factor(data

Re: [R] Re : Adding column sum to new row in data frame

2010-05-19 Thread Joshua Wiley
Dear Mohan, Is this what you want? rbind(data, c("Total",apply(data[,-1], 2, sum, na.rm=TRUE))) If your State column is a factor, it will return a warning that NAs were introduced (but the totals will still be at the bottom). If State is class character, then that row will have a name "Total" a

[R] Re : Adding column sum to new row in data frame

2010-05-19 Thread Mohan L
Dear All, I have data some thing like this: State Jan Feb Mar Apr May Jun AAA 1 1 0 2 2 0 BBB 1298 1195 1212 1244 1158 845 CCC 0 0 0 1 2 1 DDD 5 11 17 15 10 9 EEE 18 28 27 23 23 16 FFF 68 152 184 135 111 86 I want to sum all the column(Jan, Feb, Mar ...) and have to merge the total at