G.Maubach at weinwolf.de
2016-Jun-16 12:57 UTC
[R] Building a binary vector out of dichotomous variables
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
Tom Wright
2016-Jun-16 14:13 UTC
[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.
G.Maubach at gmx.de
2016-Jun-17 07:19 UTC
[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. > >
PIKAL Petr
2016-Jun-17 08:34 UTC
[R] Fw: Aw: Re: Building a binary vector out of dichotomous variables
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.