[R] Odp: Problem in converting natural numbers to bits and others
Vincent Goulet
vincent.goulet at act.ulaval.ca
Wed May 21 16:26:01 CEST 2008
Le mer. 21 mai à 04:38, Petr PIKAL a écrit :
> Hallo
>
> r-help-bounces at r-project.org napsal dne 21.05.2008 09:42:40:
>
>> Hi,
>>
>> I just started using R for about one week and I have few problems.
>>
>> i)I have a problem in finding right function to convert a table of
> natural
>> numbers to bitwise. For a simple example;
>>
>> I have the below table:-
>>
>> Column Col1 Col2 Col3
>> Sample1 5 7 10
>> Sample2 0 2 1
>> Sample3 4 0 0
>>
>>
>> Supposedly i wanted to convert to :-
>>
>>
>> Column Col1 Col2 Col3
>> Sample1 1 1 1
>> Sample2 0 1 1
>> Sample3 1 0 0
>
> data<-read.table("clipboard", header=T
> If data is your data frame,
>
> data.bit<-data.frame(column=data[,1],(data[,-1]>0)*1)
sign(data) will also do.
Vincent
>
>
>>
>> ii)Besides, I would like to create a formula of Sum(3*(Element in
>> each
>> column for a row))..does it equivalent to 3*(data[1:3,1:2]) in this
> case?or
>> I need to have Sum(3*(data[1:3,1:2])). Basically I wanted to mutliply
> each
>> element of column table of a row and sum it up.
>
> Rather complicated.
>
>> 3*(data[1:3,2:3])
> Col1 Col2
> 1 15 21
> 2 0 6
> 3 12 0
>> sum(3*(data[1:3,2:3]))
> [1] 54
>
> Works but I am suspicious that you wont something else.
>
>
>>
>> iii)I would also like to know how to insert an if else statement
>> where
> in
>> the above case, I wanted to check if data[1:3,]
>> returns me 1 or 0, it will execute some calculations.
>> I refer to the R intro pdf and it mentioned using if (expr_1 ) expr_2
> else
>> expr_3. Does that mean if(data[1:3,1:2])>0 output[1:3,1:2]=0
>> else output[1:3,1:2]="100". If I did this command, how does R knows
> which
>> row and column to map the output with?
>
> If ... else ... is usually for working as programming steps
>
> ifelse(..., ..., ...)
>
> works with objects and is parts. You shall think first how to do your
> calculations with whole objects, it is usually more effective.
>
> Regards
> Petr
>
>>
>> Please advise. Appreciate alot. 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.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list