Jason Lee
2008-May-21 07:42 UTC
[R] Problem in converting natural numbers to bits and others
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 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. 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? Please advise. Appreciate alot. Thanks. [[alternative HTML version deleted]]
Petr PIKAL
2008-May-21 08:38 UTC
[R] Odp: Problem in converting natural numbers to bits and others
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 ofnatural> 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 0data<-read.table("clipboard", header=T If data is your data frame, data.bit<-data.frame(column=data[,1],(data[,-1]>0)*1)> > 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 thiscase?or> I need to have Sum(3*(data[1:3,1:2])). Basically I wanted to mutliplyeach> 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 wherein> 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_2else> 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 knowswhich> 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Vincent Goulet
2008-May-21 14:26 UTC
[R] Odp: Problem in converting natural numbers to bits and others
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.
Apparently Analagous Threads
- computing marginal values based on multiple columns?
- conditional filter resulting in 2 new dataframes
- average columns of data frame corresponding to replicates
- Converting Strings to Variable names
- phantom NA/NaN/Inf in foreign function call (or something altogether different?)