Hello, I have a dataframe with positive integers and for every value > 0 I would like to have 1 and the rest should be zero. For instance 1 0 1 0 1 1 with df as reference.> dfV1 V2 V3 yes 23 0 5 no 0 5 7> dput (df)structure(list(V1 = c(23, 0), V2 = c(0, 5), V3 = c(5, 7)), .Names = c("V1", "V2", "V3"), row.names = c("yes", "no"), class = "data.frame") Is there a special function? Thanks Wim [[alternative HTML version deleted]]
On 02/05/2013 05:33 AM, Wim Kreinen wrote:> Hello, > > I have a dataframe with positive integers and > for every value> 0 > I would like to have 1 and the rest should be zero. > > For instance > > 1 0 1 > 0 1 1 > > with df as reference. > >> df > V1 V2 V3 > yes 23 0 5 > no 0 5 7 >> dput (df) > structure(list(V1 = c(23, 0), V2 = c(0, 5), V3 = c(5, 7)), .Names = c("V1", > "V2", "V3"), row.names = c("yes", "no"), class = "data.frame") > > Is there a special function? >Hi Wim, Does this do what you want? newdf<-matrix(as.numeric(df>0),nrow=2) Jim
Dear Wim, You could try 1*(df > 0) HTH, Jorge.- On Tue, Feb 5, 2013 at 5:33 AM, Wim Kreinen <> wrote:> Hello, > > I have a dataframe with positive integers and > for every value > 0 > I would like to have 1 and the rest should be zero. > > For instance > > 1 0 1 > 0 1 1 > > with df as reference. > > > df > V1 V2 V3 > yes 23 0 5 > no 0 5 7 > > dput (df) > structure(list(V1 = c(23, 0), V2 = c(0, 5), V3 = c(5, 7)), .Names = c("V1", > "V2", "V3"), row.names = c("yes", "no"), class = "data.frame") > > Is there a special function? > > Thanks > Wim > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hi, You could try: ?df[df>0]<-1 #or ?df[]<-as.integer(df!=0) ?df # ? ?V1 V2 V3 #yes ?1 ?0 ?1 #no ? 0 ?1 ?1 A.K. ----- Original Message ----- From: Wim Kreinen <wkreinen at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, February 4, 2013 1:33 PM Subject: [R] Transform every integer to 1 or 0 Hello, I have a dataframe with positive integers and for every value > 0 I would like to have 1 and the rest should be zero. For instance 1 0 1 0 1 1 with df as reference.> df? ? V1 V2 V3 yes 23? 0? 5 no? 0? 5? 7> dput (df)structure(list(V1 = c(23, 0), V2 = c(0, 5), V3 = c(5, 7)), .Names = c("V1", "V2", "V3"), row.names = c("yes", "no"), class = "data.frame") Is there a special function? Thanks Wim ??? [[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.