Skip to content

How do I assign boolean (o,1) values to a column?

8 messages · Bert Gunter, David Winsemius, Xenimes +1 more

#
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:
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

  
    
#
Hi CAR,
On Mon, May 23, 2011 at 10:52 AM, CAR <acasusa at gmail.com> wrote:
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
#
On May 23, 2011, at 10:52 AM, CAR wrote:

            
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
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.
#
Hi,
On Tue, May 24, 2011 at 9:46 AM, Xenimes <acasusa at gmail.com> wrote:
That's nice, but as I said before: please use 'dput' or 'dump' on your
data.frame (or some reasonable portion of it) and paste that result
into an email so that anyone trying to help you can copy that and
paste it into an R session so they can more easily help you.

-steve

  
    
#
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.