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

David Winsemius dwinsemius at comcast.net
Tue May 24 11:03:57 CEST 2011


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



More information about the R-help mailing list