An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140220/c6cf7ffe/attachment.pl>
Data manipulation in a data.frame
8 messages · arun, Ioanna Ioannou, Bert Gunter
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.
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.
... and yet another approach (written for generalization)
names(A)[-1][as.matrix(A[,-1])%*%(seq_len(ncol(A)-1))]
[1] "D" "B" "B" "B" "C" "C" "D" Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." H. Gilbert Welch
On Thu, Feb 20, 2014 at 4:19 PM, arun <smartpink111 at yahoo.com> wrote:
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. ______________________________________________ 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.
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.
This is easy to do in the approach I showed. Instead of:
names(A)[-1][as.matrix(A[,-1])%*%(seq_len(ncol(A)-1))]
modify it to:
c("none",names(A)[-1])[as.matrix(A[,-1])%*%seq_len(ncol(A)-1)+1]
[1] "D" "B" "B" "B" "C" "C" "D" "none" Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." H. Gilbert Welch
On Fri, Feb 21, 2014 at 1: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. ______________________________________________ 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.
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.
This merely translates the matrix multiplication I used into explicit arithmetic! Nor does it generalize without extra manipulation to get the correct arithmetic expression. -- Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." H. Gilbert Welch
On Fri, Feb 21, 2014 at 9:25 AM, arun <smartpink111 at yahoo.com> wrote:
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.