[R] Fw: Aw: Re: Building a binary vector out of dichotomous variables
PIKAL Petr
petr.pikal at precheza.cz
Fri Jun 17 10:34:30 CEST 2016
Hi
Your approach seems to me rather tricky
This should work
make_bv <- function(dataset, input_variables) {
dd <- which(names(dataset) %in% input_variables)
dat<-dataset[,dd]
x <- 10^(ncol(dat):0)
result <- cbind(dataset, binary_vec=x[1]+rowSums(sweep(dat, 2, x[-1], "*")))
result}
make_bv(dataset = ds_example, input_variables = c("year2013", "year2015"))
Cheers
Petr
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of
> G.Maubach at gmx.de
> Sent: Friday, June 17, 2016 9:19 AM
> To: r-help at r-project.org
> Subject: [R] Fw: Aw: Re: Building a binary vector out of dichotomous variables
>
> > Hi Tom,
> >
> > thanks for your reply.
> >
> > Yes, that's exactly what I am looking for. I did not know about the
> automatic type conversion in R.
> >
> > #-- cut --
> > ds_example <-
> > structure(
> > list(
> > year2013 = c(0, 0, 0, 1, 1, 1, 1, 0),
> > year2014 = c(0,
> > 0, 1, 1, 0, 0, 1, 1),
> > year2015 = c(0, 1, 1, 1, 0, 1, 0, 0)
> > ),
> > .Names = c("year2013",
> > "year2014", "year2015"),
> > row.names = c(NA, 8L),
> > class = "data.frame"
> > )
> >
> > #-- Proposal: works!
> > as.numeric(with(ds_example,paste(1,year2013,year2014,year2015,sep=''))
> > )
> >
> > # I store my know-how about R in functions for later use.
> >
> > #--´ Putting it in a function - does not work!
> > t_make_binary_vector <- function(dataset,
> > input_variables,
> > output_variable = "binary_vector") {
> > dataset[output_variable] <- "1"
> > print(dataset[output_variable])
> >
> > for (variable in input_variables) {
> > print(variable)
> > dataset[output_variable] <- paste(dataset[output_variable],
> > dataset[variable],
> > sep='')
> > }
> >
> > # print(dataset[output_variable])
> >
> > dataset[output_variable] <- as.integer(dataset[output_variable])
> >
> > return(dataset)
> > }
> >
> > t_make_binary_vector(dataset = ds_example,
> > input_variables = c("year2013", "year2014", "year2015"),
> > output_variable = "binary_vector")
> >
> >
> > #-- Doesn't work either.
> > t_make_binary_vector <- function(dataset,
> > input_variables,
> > output_variable = "binary_vector") {
> > dataset[output_variable] <- as.integer(paste(1, dataset[ ,
> > input_variables], sep = ''))
> >
> > return(dataset)
> > }
> >
> > t_make_binary_vector(dataset = ds_example,
> > input_variables = c("year2013", "year2014", "year2015"),
> > output_variable = "binary_vector")
> >
> > #-- cut --
> >
> > Why is R taking the parameter value itself to paste it together instead of
> referencing the variable within the dataset?
> >
> > What did I get wrong about R? How can I fix it?
> >
> > Kind regards
> >
> > Georg
> >
> >
> > > Gesendet: Donnerstag, 16. Juni 2016 um 16:13 Uhr
> > > Von: "Tom Wright" <tom at maladmin.com>
> > > An: G.Maubach at weinwolf.de
> > > Cc: "R. Help" <r-help at r-project.org>
> > > Betreff: Re: [R] Building a binary vector out of dichotomous
> > > variables
> > >
> > > Does this do what you want?
> > >
> > > as.numeric(with(ds_example,paste(1,year2013,year2014,year2015,sep=''
> > > )))
> > >
> > > On Thu, Jun 16, 2016 at 8:57 AM, <G.Maubach at weinwolf.de> wrote:
> > > > Hi All,
> > > >
> > > > I need to build a binary vector made of a set of dichotomous variables.
> > > >
> > > > What I have so far is:
> > > >
> > > > -- cut --
> > > >
> > > > ds_example <-
> > > > structure(
> > > > list(
> > > > year2013 = c(0, 0, 0, 1, 1, 1, 1, 0),
> > > > year2014 = c(0,
> > > > 0, 1, 1, 0, 0, 1, 1),
> > > > year2015 = c(0, 1, 1, 1, 0, 1, 0, 0)
> > > > ),
> > > > .Names = c("year2013",
> > > > "year2014", "year2015"),
> > > > row.names = c(NA, 8L),
> > > > class = "data.frame"
> > > > )
> > > >
> > > > attach(ds_example)
> > > > base <- 1000
> > > > binary_vector <- base + year2013 * 100 + year2014 * 10 + year2015
> > > > detach(ds_example)
> > > >
> > > > binary_vector
> > > >
> > > > ds_example <- cbind(ds_example, binary_vector)
> > > >
> > > > varlist <- c("year2013", "year2014", "year2015")
> > > >
> > > > base <- 10^length(varlist)
> > > >
> > > > binary_vector <- NULL
> > > >
> > > > for (i in 1:3) {
> > > > binary_vector <-
> > > > base +
> > > > ds_example [[varlist[i]]] * base / (10 ^ i) }
> > > >
> > > > ds_example <- cbind(ds_example, binary_vector)
> > > >
> > > > message("Wrong result!")
> > > > ds_example
> > > >
> > > > -- cut --
> > > >
> > > > How do I get vectors like 1000 1001 1011 1111 1100 1101 1110 1010
> > > > for each case?
> > > >
> > > > Is there a better approach than mine?
> > > >
> > > > Kind regards
> > > >
> > > > Georg
> > > >
> > > > ______________________________________________
> > > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > > > 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 -- To UNSUBSCRIBE and more, see
> > > 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 -- To UNSUBSCRIBE and more, see
> 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.
________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.
V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.
This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.
In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
More information about the R-help
mailing list