Data manipulation in a data.frame
Hi IOanna,
Do you have rows with multiple '1's?? If not, you could also try:
A$Variable <- c("none",names(A)[-1])[1+with(A,B+2*C+3*D)]
A.K.
On Friday, February 21, 2014 4:44 AM, ioanna ioannou <ii54250 at msn.com> wrote:
Thank you very much. One further question. Assuming that for some points there is no classification for example: A<-data.frame(A=c(10,100,1000,30,50,60,300,3), ? ? ? ? ? ? ? B=c(0,1,1,1,0,0,0,0),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? C=c(0,0,0,0,1,1,0,0), ? ? ? ? ? ? ? D=c(1,0,0,0,0,0,1,0)) Is there an easy way to introduce an extra none option in the variable? A<-data.frame(A=c(10,100,1000,30,50,60,300,3), ? ? ? ? ? ? ? B=c(0,1,1,1,0,0,0,0),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? C=c(0,0,0,0,1,1,0,0), ? ? ? ? ? ? ? D=c(1,0,0,0,0,0,1,0), ? ? ? Variable=c(D,B,B,B,C,C,D,none)) Thanks in advance, IOanna -----Original Message----- From: arun [mailto:smartpink111 at yahoo.com] Sent: 21 February 2014 00:19 To: r-help at r-project.org Cc: ioanna ioannou Subject: Re: [R] Data manipulation in a data.frame Also, rownames(which(t(!!A[,-1]),arr.ind=TRUE)) A.K.
On Thursday, February 20, 2014 6:48 PM, arun <smartpink111 at yahoo.com> wrote:
Hi, May be this helps: A$Variable <- rep(colnames(A[,-1]),nrow(A))[t(!!A[,-1])] A.K. On Thursday, February 20, 2014 5:55 PM, ioanna ioannou <ii54250 at msn.com> wrote: Hello, Assuming that I have a data frame A<-data.frame(A=c(10,100,1000,30,50,60,300), ? ? ? ? ? ? ? B=c(0,1,1,1,0,0,0),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? C=c(0,0,0,0,1,1,0), ? ? ? ? ? ? ? D=c(1,0,0,0,0,0,1)) What I would like is to introduce a new column Variable such that: A<-data.frame(A=c(10,100,1000,30,50,60,300), ? ? ? ? ? ? ? B=c(0,1,1,1,0,0,0),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? C=c(0,0,0,0,1,1,0), ? ? ? ? ? ? ? D=c(1,0,0,0,0,0,1), ? ? ?? Variable=c(D,B,B,B,C,C,D)) How can I do it? Best IOanna ??? [[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.