[R] Fw: Aw: Re: Building a binary vector out of dichotomous variables

G.Maubach at gmx.de G.Maubach at gmx.de
Fri Jun 17 09:19:10 CEST 2016


> 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.
> >



More information about the R-help mailing list