Hi, I have 6 columns in my dataframe (test) A B C D E F a b c d 40 30 a f a b 20 10 x m y m 50 30 If character value in column A matches with that in column C, it gets the percentage in column E, similarly if value in column B matches the value in D, it gets the percentage in F column. If it doesn't match, it gets 0 percentage. How can I do that ? thanks [[alternative HTML version deleted]]
Hi> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Sapana Lohani > Sent: Tuesday, August 28, 2012 7:01 AM > To: R help > Subject: [R] check and verify > > Hi, > > > I have 6 columns in my dataframe (test) > > A B C D E F > a b c d 40 30 > a f a b 20 10 > x m y m 50 30 > > If character value in column A matches with that in column C, it gets > the percentage in column E, similarly if value in column B matches the > value in D, it gets the percentage in F column. If it doesn't match, it > gets 0 percentage. > > > How can I do that ?test<-read.table("clipboard", header=T) test A B C D E F 1 a b c d 40 30 2 a f a b 20 10 3 x m y m 50 30 test[,3]%in%test[,1]*test[,5] [1] 0 20 0 test[,4]%in%test[,2]*test[,6] [1] 0 10 30 Is this what you want? Regards Petr> > thanks > > [[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.
?ifelse Probably most simply used via ?with, e.g.l result1 <- with(test, ifelse(A == C,E,0)) ## Similarly for B and D -- Bert On Mon, Aug 27, 2012 at 10:00 PM, Sapana Lohani <lohani.sapana at ymail.com> wrote:> Hi, > > > I have 6 columns in my dataframe (test) > > A B C D E F > a b c d 40 30 > a f a b 20 10 > x m y m 50 30 > > If character value in column A matches with that in column C, it gets the percentage in column E, similarly if value in column B matches the value in D, it gets the percentage in F column. If it doesn't match, it gets 0 percentage. > > > How can I do that ? > > thanks > > [[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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Hi, Try this: ?test$A<-(test$A==test$C)*test$E ?test$B<-(test$B==test$D)*test$F ?test #?? A? B C D? E? F #1? 0? 0 c d 40 30 #2 20? 0 a b 20 10 #3? 0 30 y m 50 30 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ?within(test,{A<-(A==C)*E; B<-(B==D)*F}) ?#? A? B C D? E? F #1? 0? 0 c d 40 30 #2 20? 0 a b 20 10 #3? 0 30 y m 50 30 A.K. ----- Original Message ----- From: Sapana Lohani <lohani.sapana at ymail.com> To: R help <r-help at r-project.org> Cc: Sent: Tuesday, August 28, 2012 1:00 AM Subject: [R] check and verify Hi, I have 6 columns in my dataframe (test) A B C D E F a b c d 40 30 a f a b 20 10 x m y m 50 30 If character value in column A matches with that in column C, it gets the percentage in column E, similarly if value in column B matches the value in D, it gets the percentage in F column. If it doesn't match, it gets 0 percentage. How can I do that ? thanks ??? [[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.
Hi, may be i was not clear when I made this query before. I have 6 columns in my dataframe (test) A B C D E F a b c d 40 30 a f a b 20 10 x m y m 50 30 I want a new column which gets value in E if character value in column A matches with that in column C. One more column comparing B and D. It gets the percentage in F column if value in column B matches the value in D. If it doesn't match, it gets 0 percentage. I tried the ?with, ?within and ?ifelse as suggested but am getting the error saying "Error in Ops.factor(test$A, test$C) : level sets of factors are different" How do i fix this ? Basically I want my result to look like A B C D E F new_column_1 new_column_2 a b c d 40 30 0 0 a f a b 20 10 20 0 x m y m 50 30 0 30 thanks [[alternative HTML version deleted]]
HI, Try this: test<-read.table(text=" ?A B C D E F ?a b c d 40 30 ?a f a b 20 10 ?x m y m 50 30 ",sep="",header=TRUE,stringsAsFactors=FALSE) #Here, stringsAsFactors=FALSE is the key here.? If you did not add that (or =TRUE) , by default, A-D columns will be factors and you will get the error message as described. within(test,{new_column_2<-(B==D)*F;new_column_1<-(A==C)*E}) # A B C D? E? F new_column_1 new_column_2 #1 a b c d 40 30??????????? 0??????????? 0 #2 a f a b 20 10?????????? 20??????????? 0 #3 x m y m 50 30??????????? 0?????????? 30 A.K. ? ________________________________ From: Sapana Lohani <lohani.sapana at ymail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, August 28, 2012 5:40 PM Subject: Re: [R] check and verify Hi, may be i was not clear when I made this query before. I have 6 columns in my dataframe (test) A B C D E F a b c d 40 30 a f a b 20 10 x m y m 50 30 I want a new column which gets value in E if character value in column A matches with that in column C. One more column comparing B and D. It gets the percentage in F column if value in column B matches the value in D. If it doesn't match, it gets 0 percentage. I tried the ?with, ?within and ?ifelse as suggested but am getting the error saying "Error in Ops.factor(test$A, test$C) : level sets of factors are different" How do i fix this ? Basically I want my result to look like A B C D E F new_column_1 new_column_2 a b c d 40 30 0 0 a f a b 20 10 20 0 x m y m 50 30 0 30 Could you please help me fix the error? thanks ________________________________ From: arun <smartpink111 at yahoo.com> To: Sapana Lohani <lohani.sapana at ymail.com> Cc: R help <r-help at r-project.org> Sent: Tuesday, August 28, 2012 5:49 AM Subject: Re: [R] check and verify Hi, Try this: ?test$A<-(test$A==test$C)*test$E ?test$B<-(test$B==test$D)*test$F ?test #?? A? B C D? E? F #1? 0? 0 c d 40 30 #2 20? 0 a b 20 10 #3? 0 30 y m 50 30 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ?within(test,{A<-(A==C)*E; B<-(B==D)*F}) ?#? A? B C D? E? F #1? 0? 0 c d 40 30 #2 20? 0 a b 20 10 #3? 0 30 y m 50 30 A.K. ----- Original Message ----- From: Sapana Lohani <lohani.sapana at ymail.com> To: R help <r-help at r-project.org> Cc: Sent: Tuesday, August 28, 2012 1:00 AM Subject: [R] check and verify Hi, I have 6 columns in my dataframe (test) A B C D E F a b c d 40 30 a f a b 20 10 x m y m 50 30 If character value in column A matches with that in column C, it gets the percentage in column E, similarly if value in column B matches the value in D, it gets the percentage in F column. If it doesn't match, it gets 0 percentage. How can I do that ? thanks ??? [[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.
HI, If you wish to keep it as factors (A-D), then try this: test<-read.table(text=" ?A B C D E F ?a b c d 40 30 ?a f a b 20 10 ?x m y m 50 30 ",sep="",header=TRUE) str(test) #'data.frame':??? 3 obs. of? 6 variables: # $ A: Factor w/ 2 levels "a","x": 1 1 2 # $ B: Factor w/ 3 levels "b","f","m": 1 2 3 # $ C: Factor w/ 3 levels "a","c","y": 2 1 3 # $ D: Factor w/ 3 levels "b","d","m": 2 1 3 # $ E: int? 40 20 50 # $ F: int? 30 10 30 ?test$new_column_1<-(as.character(test$A)==as.character(test$C))*test$E ?test$new_column_2<-(as.character(test$B)==as.character(test$D))*test$F test #? A B C D? E? F new_column_1 new_column_2 #1 a b c d 40 30??????????? 0??????????? 0 #2 a f a b 20 10?????????? 20??????????? 0 #3 x m y m 50 30??????????? 0?????????? 30 #or within(test,{new_column_2<-(as.character(B)==as.character(D))*F;new_column_1<-(as.character(A)==as.character(C))*E}) ? #A B C D? E? F new_column_1 new_column_2 #1 a b c d 40 30??????????? 0??????????? 0 #2 a f a b 20 10?????????? 20??????????? 0 #3 x m y m 50 30??????????? 0?????????? 30 A.K. ________________________________ From: Sapana Lohani <lohani.sapana at ymail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, August 28, 2012 5:40 PM Subject: Re: [R] check and verify Hi, may be i was not clear when I made this query before. I have 6 columns in my dataframe (test) A B C D E F a b c d 40 30 a f a b 20 10 x m y m 50 30 I want a new column which gets value in E if character value in column A matches with that in column C. One more column comparing B and D. It gets the percentage in F column if value in column B matches the value in D. If it doesn't match, it gets 0 percentage. I tried the ?with, ?within and ?ifelse as suggested but am getting the error saying "Error in Ops.factor(test$A, test$C) : level sets of factors are different" How do i fix this ? Basically I want my result to look like A B C D E F new_column_1 new_column_2 a b c d 40 30 0 0 a f a b 20 10 20 0 x m y m 50 30 0 30 Could you please help me fix the error? thanks ________________________________ From: arun <smartpink111 at yahoo.com> To: Sapana Lohani <lohani.sapana at ymail.com> Cc: R help <r-help at r-project.org> Sent: Tuesday, August 28, 2012 5:49 AM Subject: Re: [R] check and verify Hi, Try this: ?test$A<-(test$A==test$C)*test$E ?test$B<-(test$B==test$D)*test$F ?test #?? A? B C D? E? F #1? 0? 0 c d 40 30 #2 20? 0 a b 20 10 #3? 0 30 y m 50 30 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ?within(test,{A<-(A==C)*E; B<-(B==D)*F}) ?#? A? B C D? E? F #1? 0? 0 c d 40 30 #2 20? 0 a b 20 10 #3? 0 30 y m 50 30 A.K. ----- Original Message ----- From: Sapana Lohani <lohani.sapana at ymail.com> To: R help <r-help at r-project.org> Cc: Sent: Tuesday, August 28, 2012 1:00 AM Subject: [R] check and verify Hi, I have 6 columns in my dataframe (test) A B C D E F a b c d 40 30 a f a b 20 10 x m y m 50 30 If character value in column A matches with that in column C, it gets the percentage in column E, similarly if value in column B matches the value in D, it gets the percentage in F column. If it doesn't match, it gets 0 percentage. How can I do that ? thanks ??? [[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.
HI, I just answered to one with similar contents but with a different title.? I guess both are the same. A.K. ----- Original Message ----- From: Sapana Lohani <lohani.sapana at ymail.com> To: R help <r-help at r-project.org> Cc: Sent: Tuesday, August 28, 2012 5:41 PM Subject: [R] Error in ops.factor: check and verify Hi, may be i was not clear when I made this query before. I have 6 columns in my dataframe (test) A B C D E F a b c d 40 30 a f a b 20 10 x m y m 50 30 I want a new column which gets value in E if character value in column A matches with that in column C. One more column comparing B and D. It gets the percentage in F column if value in column B matches the value in D. If it doesn't match, it gets 0 percentage. I tried the ?with, ?within and ?ifelse as suggested but am getting the error saying "Error in Ops.factor(test$A, test$C) : level sets of factors are different" How do i fix this ? Basically I want my result to look like A B C D E F new_column_1 new_column_2 a b c d 40 30 0 0 a f a b 20 10 20 0 x m y m 50 30 0 30 thanks ??? [[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.