I am new to R. I have a data like: x y z w p .......... m 1 10 15 20 25 30 2 11 16 21 26 31 3 12 17 18 19 20 4 51 52 53 55 67 ....... thus I have 145 rows and 160 column in my data which is named as data.csv. Now i want to create a new column 'm' and for every row m will take value =column 7+ column 12+ column 57+ column 45 i.e. for every row it will take value of sum of corresponding row's 7 & 12 & 57 & 45 column's value . So, how to write the code for this operation? -- RAJIB PRASAD Centre for Economic Studies & Planning Jawaharlal Nehru University New Delhi-67 contact no: 09868320368 mail id: rwho2007 at gmail.com
Well if I understand correctly you want data.csv$m <- rowSums(data.csv[, c(7,12,57,45)]) If you want something else please read Posting Guide and provide at least part of your data and the result you want. In the meantime It would be good for you to read at least R intro document provided with your R installation. Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of rajib prasad > Sent: Friday, August 23, 2013 12:18 PM > To: r-help at r-project.org > Subject: [R] (no subject) > > I am new to R. I have a data like: > > x y z w p > .......... m > 1 10 15 20 25 30 > 2 11 16 21 26 31 > 3 12 17 18 19 20 > 4 51 52 53 55 67 > ....... > > thus I have 145 rows and 160 column in my data which is named as > data.csv. Now i want to create a new column 'm' and for every row m > will take value =column 7+ column 12+ column 57+ column 45 i.e. for > every row it will take value of sum of corresponding row's 7 & 12 & 57 > & 45 column's value . > So, how to write the code for this operation? > > > > > > -- > > > > RAJIB PRASAD > > Centre for Economic Studies & Planning > Jawaharlal Nehru University > New Delhi-67 > contact no: 09868320368 > mail id: rwho2007 at gmail.com > > ______________________________________________ > 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.
You can easily subset the data then use rowSum. say your dataset name is data1. then write data2<-data[,c(7,12,45,57)] then write result<-rowsum(data2) On Fri, Aug 23, 2013 at 3:47 PM, rajib prasad <rwho2007@gmail.com> wrote:> I am new to R. I have a data like: > > x y z w p .......... > m > 1 10 15 20 25 30 > 2 11 16 21 26 31 > 3 12 17 18 19 20 > 4 51 52 53 55 67 > ....... > > thus I have 145 rows and 160 column in my data which is named as > data.csv. Now i want to create a new column 'm' and for every row m > will take value =column 7+ column 12+ column 57+ column 45 i.e. for > every row it will take value of sum of corresponding row's 7 & 12 & 57 > & 45 column's value . > So, how to write the code for this operation? > > > > > > -- > > > > RAJIB PRASAD > > Centre for Economic Studies & Planning > Jawaharlal Nehru University > New Delhi-67 > contact no: 09868320368 > mail id: rwho2007@gmail.com > > ______________________________________________ > R-help@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. >-- Anindya Sankar Dey [[alternative HTML version deleted]]
Hi, May be: ?set.seed(24) ?dat1<- as.data.frame(matrix(sample(1:50,145*160,replace=TRUE),ncol=160)) dat2<- dat1 dat1$m<-rowSums(dat1[,c(7,12,45,57)]) #or dat2<-within(dat2,{m<-rowSums(cbind(V7,V12,V45,V57))}) #using column names identical(dat1,dat2) #[1] TRUE A.K. ----- Original Message ----- From: rajib prasad <rwho2007 at gmail.com> To: r-help at r-project.org Cc: Sent: Friday, August 23, 2013 6:17 AM Subject: [R] (no subject) I am new to R. I have a data like: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x? ? ? y? ? ? z? ? ? w? ? ? p? ..........? ? ? m ? ? ? ? ? ? ? ? ? ? ? 1? ? 10? ? 15? ? 20? ? 25? ? 30 ? ? ? ? ? ? ? ? ? ? ? 2? ? 11? ? 16? ? 21? ? 26? ? 31 ? ? ? ? ? ? ? ? ? ? ? 3? ? ? 12? ? 17? ? 18? ? 19? ? 20 ? ? ? ? ? ? ? ? ? ? ? 4? ? ? 51? ? 52? ? 53? ? 55? ? 67 ? ? ? ? ? ? ? ? ....... thus I have 145 rows and 160 column in my data which is named as data.csv.? Now i want to create a new column 'm' and for every row m will take value =column 7+ column 12+ column 57+ column 45 i.e. for every row it will take value of sum of corresponding row's 7 & 12 & 57 & 45 column's value . So, how to write the code for this operation? -- RAJIB PRASAD Centre for Economic Studies & Planning Jawaharlal Nehru University New Delhi-67 contact no: 09868320368 mail id: rwho2007 at gmail.com ______________________________________________ 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.