Hermann Norpois
2013-Mar-29 19:01 UTC
[R] From a vector with characters to binary information
Hello, I would like to transform a character vector into a "binary" vector ("keine" and " " become 0 and the rest 1).> dput (scm)c("keine", " ", "keine", "Erstgradverw.", "Mutter", "Erstgradverw.", "Erstgradverw.", "keine", " ", "Vater", "Erstgradverw.", "keine", "keine", "keine", "keine", " ", "Erstgradverw.", "keine", "keine", "Erstgradverw.") I tried: scoref <- sapply (scm, function (x) ifelse (x == "keine"|x==" ", 0, 1)) or scoref <- sapply (scm, function (x) ifelse (x == "keine"|x==" ", x <- 0, x <- 1)) And this is the output:> scorefkeine keine Erstgradverw. Mutter 0 0 0 1 1 Erstgradverw. Erstgradverw. keine Vater 1 1 0 0 1 Erstgradverw. keine keine keine keine 1 0 0 0 0 Erstgradverw. keine keine Erstgradverw. 0 1 0 0 1 How do I get rid off the text. Is there an alternative method? Thanks Hermann [[alternative HTML version deleted]]
Berend Hasselman
2013-Mar-30 08:59 UTC
[R] From a vector with characters to binary information
On 29-03-2013, at 20:01, Hermann Norpois <hnorpois at gmail.com> wrote:> Hello, > > I would like to transform a character vector into a "binary" vector > ("keine" and " " become 0 and the rest 1). > >> dput (scm) > c("keine", " ", "keine", "Erstgradverw.", "Mutter", "Erstgradverw.", > "Erstgradverw.", "keine", " ", "Vater", "Erstgradverw.", "keine", > "keine", "keine", "keine", " ", "Erstgradverw.", "keine", "keine", > "Erstgradverw.") > > I tried: > > scoref <- sapply (scm, function (x) ifelse (x == "keine"|x==" ", 0, 1)) > > or > > scoref <- sapply (scm, function (x) ifelse (x == "keine"|x==" ", x <- 0, x > <- 1)) > > And this is the output: > >> scoref > keine keine Erstgradverw. Mutter > 0 0 0 1 1 > Erstgradverw. Erstgradverw. keine Vater > 1 1 0 0 1 > Erstgradverw. keine keine keine keine > 1 0 0 0 0 > Erstgradverw. keine keine Erstgradverw. > 0 1 0 0 1 > > How do I get rid off the text. Is there an alternative method? >First of all read the help page for sapply (and other apply's) in particular the part describing argument USE.NAMES. You don't need to use ifelse: a simple if/else will do. x is a scalar in your function and ifelse is overkill. So you also don't need to use |. So scoref <- sapply (scm, function (x) if (x == "keine" || x==" ") 0 else 1, USE.NAMES=FALSE) or scoref <- sapply (scm, function (x) 1-(x == "keine"||x==" "), USE.NAMES=FALSE) Berend
Berend Hasselman
2013-Mar-30 09:19 UTC
[R] From a vector with characters to binary information
On 29-03-2013, at 20:01, Hermann Norpois <hnorpois at gmail.com> wrote:> Hello, > > I would like to transform a character vector into a "binary" vector > ("keine" and " " become 0 and the rest 1). > >> dput (scm) > c("keine", " ", "keine", "Erstgradverw.", "Mutter", "Erstgradverw.", > "Erstgradverw.", "keine", " ", "Vater", "Erstgradverw.", "keine", > "keine", "keine", "keine", " ", "Erstgradverw.", "keine", "keine", > "Erstgradverw.") > > I tried: > > scoref <- sapply (scm, function (x) ifelse (x == "keine"|x==" ", 0, 1)) > > or > > scoref <- sapply (scm, function (x) ifelse (x == "keine"|x==" ", x <- 0, x > <- 1)) > > And this is the output: > >> scoref > keine keine Erstgradverw. Mutter > 0 0 0 1 1 > Erstgradverw. Erstgradverw. keine Vater > 1 1 0 0 1 > Erstgradverw. keine keine keine keine > 1 0 0 0 0 > Erstgradverw. keine keine Erstgradverw. > 0 1 0 0 1 > > How do I get rid off the text. Is there an alternative method?Additionally you don't really need to use sapply. Expressions like these will get you what you want ifelse(scm=="keine" | scm==" ",0,1) 1 - (scm %in% c("keine"," ")) Berend
Hi, ?1*is.na(match(scm,c("keine"," "))) # [1] 0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 0 1 0 0 1 #or ?1*(!scm%in%c("keine"," ")) ?#[1] 0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 0 1 0 0 1 A.K. ----- Original Message ----- From: Hermann Norpois <hnorpois at gmail.com> To: r-help at r-project.org Cc: Sent: Friday, March 29, 2013 3:01 PM Subject: [R] From a vector with characters to binary information Hello, I would like to transform a character vector into a "binary" vector ("keine" and " " become 0 and the rest 1).> dput (scm)c("keine", " ", "keine", "Erstgradverw.", "Mutter", "Erstgradverw.", "Erstgradverw.", "keine", " ", "Vater", "Erstgradverw.", "keine", "keine", "keine", "keine", " ", "Erstgradverw.", "keine", "keine", "Erstgradverw.") I tried: scoref <- sapply (scm, function (x) ifelse (x == "keine"|x==" ", 0, 1)) or scoref <- sapply (scm, function (x) ifelse (x == "keine"|x==" ", x <- 0, x <- 1)) And this is the output:> scoref? ? ? ? keine? ? ? ? ? ? ? ? ? ? ? keine Erstgradverw.? ? ? ? Mutter ? ? ? ? ? ? 0? ? ? ? ? ? 0? ? ? ? ? ? 0? ? ? ? ? ? 1? ? ? ? ? ? 1 Erstgradverw. Erstgradverw.? ? ? ? keine? ? ? ? ? ? ? ? ? ? ? Vater ? ? ? ? ? ? 1? ? ? ? ? ? 1? ? ? ? ? ? 0? ? ? ? ? ? 0? ? ? ? ? ? 1 Erstgradverw.? ? ? ? keine? ? ? ? keine? ? ? ? keine? ? ? ? keine ? ? ? ? ? ? 1? ? ? ? ? ? 0? ? ? ? ? ? 0? ? ? ? ? ? 0? ? ? ? ? ? 0 ? ? ? ? ? ? ? Erstgradverw.? ? ? ? keine? ? ? ? keine Erstgradverw. ? ? ? ? ? ? 0? ? ? ? ? ? 1? ? ? ? ? ? 0? ? ? ? ? ? 0? ? ? ? ? ? 1 How do I get rid off the text. Is there an alternative method? Thanks Hermann ??? [[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.