Hi, I am looking for a very easy way to transform a column in a dataframe from counts (eg. c(1,0,21,2,0,0,234,2,0)) into a binary form to get presence/absence values e.g. c(1,0,1,1,0,0,1,1,0). Is there a simple built-in function? or do I have do to it with a replaceement funciton using IF x > 0 THEN 1 etc.? /johannes -- Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a
Just use the logical operators. Counts <- c(1,0,21,2,0,0,234,2,0) Counts > 0 1 *(Counts > 0) ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium + 32 2 525 02 51 + 32 54 43 61 85 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Johannes Radinger Verzonden: donderdag 31 mei 2012 13:13 Aan: R-help at r-project.org Onderwerp: [R] Transform counts into presence/absence Hi, I am looking for a very easy way to transform a column in a dataframe from counts (eg. c(1,0,21,2,0,0,234,2,0)) into a binary form to get presence/absence values e.g. c(1,0,1,1,0,0,1,1,0). Is there a simple built-in function? or do I have do to it with a replaceement funciton using IF x > 0 THEN 1 etc.? /johannes -- Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a ______________________________________________ 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. * * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * * Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
On 05/31/2012 09:13 PM, Johannes Radinger wrote:> Hi, > > I am looking for a very easy way to transform > a column in a dataframe from counts (eg. c(1,0,21,2,0,0,234,2,0)) > into a binary form to get presence/absence values > e.g. c(1,0,1,1,0,0,1,1,0). Is there a simple built-in function? > or do I have do to it with a replaceement funciton using IF x> 0 > THEN 1 etc.? >Hi Johannes, Probably the easiest is: testvec<-c(1,0,21,2,0,0,234,2,0) newvec<-ifelse(testvec>0,1,0) Jim
Hello, Try x <- c(1,0,21,2,0,0,234,2,0) as.integer(x != 0) Hope this helps, Rui Barradas Em 31-05-2012 12:13, Johannes Radinger escreveu:> Hi, > > I am looking for a very easy way to transform > a column in a dataframe from counts (eg. c(1,0,21,2,0,0,234,2,0)) > into a binary form to get presence/absence values > e.g. c(1,0,1,1,0,0,1,1,0). Is there a simple built-in function? > or do I have do to it with a replaceement funciton using IF x> 0 > THEN 1 etc.? > > /johannes
-------- Original-Nachricht --------> Datum: Thu, 31 May 2012 11:16:32 +0000 > Von: "ONKELINX, Thierry" <Thierry.ONKELINX at inbo.be> > An: Johannes Radinger <JRadinger at gmx.at>, "R-help at r-project.org" <R-help at r-project.org> > Betreff: RE: [R] Transform counts into presence/absence> Just use the logical operators.of course, that simple :) . thank you!> > Counts <- c(1,0,21,2,0,0,234,2,0) > Counts > 0 > 1 *(Counts > 0) > > ir. Thierry Onkelinx > Instituut voor natuur- en bosonderzoek / Research Institute for Nature and > Forest > team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance > Kliniekstraat 25 > 1070 Anderlecht > Belgium > + 32 2 525 02 51 > + 32 54 43 61 85 > Thierry.Onkelinx at inbo.be > www.inbo.be > > To call in the statistician after the experiment is done may be no more > than asking him to perform a post-mortem examination: he may be able to say > what the experiment died of. > ~ Sir Ronald Aylmer Fisher > > The plural of anecdote is not data. > ~ Roger Brinner > > The combination of some data and an aching desire for an answer does not > ensure that a reasonable answer can be extracted from a given body of data. > ~ John Tukey > > > -----Oorspronkelijk bericht----- > Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > Namens Johannes Radinger > Verzonden: donderdag 31 mei 2012 13:13 > Aan: R-help at r-project.org > Onderwerp: [R] Transform counts into presence/absence > > Hi, > > I am looking for a very easy way to transform a column in a dataframe from > counts (eg. c(1,0,21,2,0,0,234,2,0)) into a binary form to get > presence/absence values e.g. c(1,0,1,1,0,0,1,1,0). Is there a simple built-in > function? > or do I have do to it with a replaceement funciton using IF x > 0 THEN 1 > etc.? > > /johannes > -- > > Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a > > ______________________________________________ > 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. > * * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * * > Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver > weer en binden het INBO onder geen enkel beding, zolang dit bericht niet > bevestigd is door een geldig ondertekend document. > The views expressed in this message and any annex are purely those of the > writer and may not be regarded as stating an official position of INBO, as > long as the message is not confirmed by a duly signed document.-- Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a
Hi I was curious on the possibilities -- what if an NA creeps in? Counts <- c(1,0,21,2,0,0,234,2,0,NA) Counts > 0 1 *(Counts > 0) [1] 1 0 1 1 0 0 1 1 0 NA as.integer(x != 0) [1] 1 0 1 1 0 0 1 1 0 Regards Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2351 Email: home: mackay at northnet.com.au At 21:27 31/05/2012, you wrote:>-------- Original-Nachricht -------- > > Datum: Thu, 31 May 2012 11:16:32 +0000 > > Von: "ONKELINX, Thierry" <Thierry.ONKELINX at inbo.be> > > An: Johannes Radinger <JRadinger at gmx.at>, "R-help at r-project.org" > <R-help at r-project.org> > > Betreff: RE: [R] Transform counts into presence/absence > > > Just use the logical operators. > > >of course, that simple :) . thank you! > > > > > > Counts <- c(1,0,21,2,0,0,234,2,0) > > Counts > 0 > > 1 *(Counts > 0) > > > > ir. Thierry Onkelinx > > Instituut voor natuur- en bosonderzoek / Research Institute for Nature and > > Forest > > team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance > > Kliniekstraat 25 > > 1070 Anderlecht > > Belgium > > + 32 2 525 02 51 > > + 32 54 43 61 85 > > Thierry.Onkelinx at inbo.be > > www.inbo.be > > > > To call in the statistician after the experiment is done may be no more > > than asking him to perform a post-mortem examination: he may be able to say > > what the experiment died of. > > ~ Sir Ronald Aylmer Fisher > > > > The plural of anecdote is not data. > > ~ Roger Brinner > > > > The combination of some data and an aching desire for an answer does not > > ensure that a reasonable answer can be extracted from a given body of data. > > ~ John Tukey > > > > > > -----Oorspronkelijk bericht----- > > Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > > Namens Johannes Radinger > > Verzonden: donderdag 31 mei 2012 13:13 > > Aan: R-help at r-project.org > > Onderwerp: [R] Transform counts into presence/absence > > > > Hi, > > > > I am looking for a very easy way to transform a column in a dataframe from > > counts (eg. c(1,0,21,2,0,0,234,2,0)) into a binary form to get > > presence/absence values e.g. c(1,0,1,1,0,0,1,1,0). Is there a > simple built-in > > function? > > or do I have do to it with a replaceement funciton using IF x > 0 THEN 1 > > etc.? > > > > /johannes > > -- > > > > Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a > > > > ______________________________________________ > > 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. > > * * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * * > > Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver > > weer en binden het INBO onder geen enkel beding, zolang dit bericht niet > > bevestigd is door een geldig ondertekend document. > > The views expressed in this message and any annex are purely those of the > > writer and may not be regarded as stating an official position of INBO, as > > long as the message is not confirmed by a duly signed document. > >-- > >Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a > >______________________________________________ >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.