Hi everybody!! I'm trying to assign boolean values to a column in a matrix depending on the number of times present in another column. I have no clue, I have been 2 days looking for a way, if somebody knows what kind of functions I need to use I will really apreciate it. example: Color Type Mark1 Mark2 Mark3 Red high 139 P alpha blue low 140 P alpha Green high 141 S alpha Yellow low 142 S beta Red high 143 P gamma If we only take into account columns Mark1, Mark2 and Mark3. I would like to ask R to write another column with 0=one; 1=more than one. If alpha is present for more than one Mark 2, so P and S, then I will like to assign value 1 if only for one then assign the value 0. Thanks for your help -- View this message in context: http://r.789695.n4.nabble.com/How-do-I-assign-boolean-o-1-values-to-a-column-tp3544304p3544304.html Sent from the R help mailing list archive at Nabble.com.
Hi: On Mon, May 23, 2011 at 7:52 AM, CAR <acasusa at gmail.com> wrote:> Hi everybody!! > > I'm trying to assign boolean values to a column in a matrix depending on the > number of times present in another column. I have no clue,Sorry, I don't have time to help. But have you read "The Introduction to R" tutorial where you might find sufficient info to help you solve problems like this? It's well written (IMHO) and not too long (also IMHO). Cheers, Bert I have been 2> days looking for a way, if somebody knows what kind of functions I need to > use I will really apreciate it. > > example: > > Color Type ?Mark1 Mark2 Mark3 > Red ? high ? ?139 ? ?P ? ? ? alpha > blue ? low ? ?140 ? ?P ? ? ? ?alpha > Green high ? 141 ? ?S ? ? ? alpha > Yellow low ? ?142 ? S ? ? ? ?beta > Red ? ?high ? 143 ? ?P ? ? ? gamma > > If we only take into account columns Mark1, Mark2 and Mark3. I would like to > ask R to write another column with 0=one; 1=more than one. > If alpha is present for more than one Mark 2, so P and S, then I will like > to assign value 1 if only for one then assign the value 0. > > Thanks for your help > > > -- > View this message in context: http://r.789695.n4.nabble.com/How-do-I-assign-boolean-o-1-values-to-a-column-tp3544304p3544304.html > Sent from the R help mailing list archive at Nabble.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. >-- "Men by nature long to get on to the ultimate truths, and will often be impatient with elementary studies or fight shy of them. If it were possible to reach the ultimate truths without the elementary studies usually prefixed to them, these would not be preparatory studies but superfluous diversions." -- Maimonides (1135-1204) Bert Gunter Genentech Nonclinical Biostatistics
Thanks for the tip!!! I read the R manual and some other basic helps. The problem here is I dont know even the name of what I?m looking for.... -- View this message in context: http://r.789695.n4.nabble.com/How-do-I-assign-boolean-o-1-values-to-a-column-tp3544304p3545484.html Sent from the R help mailing list archive at Nabble.com.
Steve Lianoglou
2011-May-24 02:59 UTC
[R] How do I assign boolean (o,1) values to a column?
Hi CAR, On Mon, May 23, 2011 at 10:52 AM, CAR <acasusa at gmail.com> wrote:> Hi everybody!! > > I'm trying to assign boolean values to a column in a matrix depending on the > number of times present in another column. I have no clue, I have been 2 > days looking for a way, if somebody knows what kind of functions I need to > use I will really apreciate it. > > example: > > Color Type ?Mark1 Mark2 Mark3 > Red ? high ? ?139 ? ?P ? ? ? alpha > blue ? low ? ?140 ? ?P ? ? ? ?alpha > Green high ? 141 ? ?S ? ? ? alpha > Yellow low ? ?142 ? S ? ? ? ?beta > Red ? ?high ? 143 ? ?P ? ? ? gamma > > If we only take into account columns Mark1, Mark2 and Mark3. I would like to > ask R to write another column with 0=one; 1=more than one. > If alpha is present for more than one Mark 2, so P and S, then I will like > to assign value 1 if only for one then assign the value 0.I'm having trouble understanding what you want the values in new column to be. Can you please provide the output of what you want your desired data.frame to look like with the new column appended? Also, for your "example" data.frame (the one you pasted above), I guess you have it in your R workspace. Let's say it is called 'my.data', can you please paste the output of the following command into the email as well: R> dput(my.data) This will provide us with a piece of text we can copy from your email and paste into our R workspace so we can get your data up and running easily. Thanks, -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology ?| Memorial Sloan-Kettering Cancer Center ?| Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
David Winsemius
2011-May-24 09:03 UTC
[R] How do I assign boolean (o,1) values to a column?
On May 23, 2011, at 10:52 AM, CAR wrote:> Hi everybody!! > > I'm trying to assign boolean values to a column in a matrix > depending on the > number of times present in another column. I have no clue, I have > been 2 > days looking for a way, if somebody knows what kind of functions I > need to > use I will really apreciate it. > > example: > > Color Type Mark1 Mark2 Mark3 > Red high 139 P alpha > blue low 140 P alpha > Green high 141 S alpha > Yellow low 142 S beta > Red high 143 P gamma > > If we only take into account columns Mark1, Mark2 and Mark3. I would > like to > ask R to write another column with 0=one; 1=more than one. > If alpha is present for more than one Mark 2, so P and S, then I > will like > to assign value 1 if only for one then assign the value 0.It sounds on a third reading of what not only I seem to be find to be a confusing problem statement that you want both Mark2 and Mark3 to define a basis for counting the number of Mark1 entries and placing the result (possibly duplicated) in another parallel column. If this is so, and the dataframe has a name which we will assume to be `tst` then try: > tst$count <- with( tst, ave(Mark1, interaction(Mark2, Mark3), FUN=length) ) > tst Color Type Mark1 Mark2 Mark3 count 1 Red high 139 P alpha 2 2 blue low 140 P alpha 2 3 Green high 141 S alpha 1 4 Yellow low 142 S beta 1 5 Red high 143 P gamma 1> > Thanks for your help > > > -- > View this message in context: http://r.789695.n4.nabble.com/How-do-I-assign-boolean-o-1-values-to-a-column-tp3544304p3544304.html > Sent from the R help mailing list archive at Nabble.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.David Winsemius, MD West Hartford, CT
Thank you David and Steve, Yes all this data are already in R and in csv files. Sorry for not being clear. I have this: Codes1and3[1:5,c(1:5)] Mark_2 pop Mark_1 Mark_3 age 1 P A1 139 alpha 2 2 P A1 140 alpha 2 3 P A1 141 gamma 2 4 S A1 142 gamma 2 5 S A1 143 alpha 2 6 T A2 144 alpha 2 7 T A2 145 alpha 2 I?m comparing Marks 1 and 3, then I need to know if the ones in Mark_2 in the general table have 1 or more interactions with Mark 3. For example here ?P? appears in the same row than ?alpha? and then in the same than ?gamma?, then there are 2 interactions, so in a Boolean code it should be 1 (more than1 interaction). The same for ?S?. But ?T? only interacts with ?alpha?, that will make it only one interaction = 0. The next colum I need is Code 1 referent to code 3 and in this case it should look like this: Code 1_3 1 1 2 1 3 1 4 1 5 1 6 0 I have tried to count but, of course in this case it will always be one, because they are linked in each row once, how can I consider the other rows? -- View this message in context: http://r.789695.n4.nabble.com/How-do-I-assign-boolean-o-1-values-to-a-column-tp3544304p3547156.html Sent from the R help mailing list archive at Nabble.com.
Thankyou very much, I managed to count he numbr of Markers 2 linked to Markers 3. And Markers 1 to Markers 3 with the aggregate function: with(data,aggregate(Marker1,list(Marker2=Marker2),length)) data2<-with(data,aggregate(Marker1,list(Marker2=Marker2,Marker3=Merker3),length)) So, now is easy I will only apply an "if" and solved. I want to thankyou Steve and David, the info you gave was actally usefull and I learned the "ave" now. I hope I can start being usefull in the R blog myself soon. Regards!!!! -- View this message in context: http://r.789695.n4.nabble.com/How-do-I-assign-boolean-o-1-values-to-a-column-tp3544304p3550309.html Sent from the R help mailing list archive at Nabble.com.