# Creating dummy variables

**URL:** <https://datashield.discourse.group/t/creating-dummy-variables/396>\
**Category:** Analyst Support\
**Created:** [18 May 2021 15:00 UTC](https://datashield.discourse.group/t/creating-dummy-variables/396 "2021-05-18T15:00:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tombishop](https://yyz2.discourse-cdn.com/free1/user_avatar/datashield.discourse.group/tombishop/32/23_2.png) [@tombishop](https://datashield.discourse.group/u/tombishop)\
**Post date:** [18 May 2021 15:00 UTC](https://datashield.discourse.group/t/creating-dummy-variables/396/1 "2021-05-18T15:00:16Z")

</div>

Hi, I am a bit rusty on some of the new DataSHIELD functionality. What is the best method to convert a factor variable (i.e. categorical with multiple levels) into dummy binary variables?

I can see there is probably a long-hand method with ds.boole, but wondered if there is a quicker way?

Thanks

---

<div class="post-metadata">

**Author:** ![demetris.avraam](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@demetris.avraam](https://datashield.discourse.group/u/demetris.avraam)\
**Post date:** [18 May 2021 15:40 UTC](https://datashield.discourse.group/t/creating-dummy-variables/396/2 "2021-05-18T15:40:34Z")

</div>

Hi Tom,

You can create the dummy variables from a categorical variable using the ds.asFactor function and setting the argument fixed.dummy.vars equal to TRUE 🙂

---

<div class="post-metadata">

**Author:** ![tombishop](https://yyz2.discourse-cdn.com/free1/user_avatar/datashield.discourse.group/tombishop/32/23_2.png) [@tombishop](https://datashield.discourse.group/u/tombishop)\
**Post date:** [18 May 2021 16:05 UTC](https://datashield.discourse.group/t/creating-dummy-variables/396/3 "2021-05-18T16:05:01Z")

</div>

Hi Demetris, that is absolutely wonderful! Thank you!

---

<div class="post-metadata">

**Author:** ![tombishop](https://yyz2.discourse-cdn.com/free1/user_avatar/datashield.discourse.group/tombishop/32/23_2.png) [@tombishop](https://datashield.discourse.group/u/tombishop)\
**Post date:** [19 May 2021 08:14 UTC](https://datashield.discourse.group/t/creating-dummy-variables/396/4 "2021-05-19T08:14:38Z")

</div>

If I do this to multiple variables, then they all have column names like DV2 DV3 etc so there is conflict. What is the easiest way to rename columns in a matrix/dataframe, please?

---

<div class="post-metadata">

**Author:** ![demetris.avraam](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@demetris.avraam](https://datashield.discourse.group/u/demetris.avraam)\
**Post date:** [19 May 2021 08:47 UTC](https://datashield.discourse.group/t/creating-dummy-variables/396/5 "2021-05-19T08:47:01Z")

</div>

Tom, the only way to rename variables using the current version of dsBaseClient is to use the ds.make function to replicate the variables with a different name and then add those variables back to your dataframe and remove the variables with the DV2 and DV3 names.

An easiest way is to use Tim’s “dh.renameVars” function that is included in the dsHelper package (this is a client-side function!)

> <https://github.com/lifecycle-project/ds-helper/blob/master/R/rename-vars.R>

---

<div class="post-metadata">

**Author:** ![tombishop](https://yyz2.discourse-cdn.com/free1/user_avatar/datashield.discourse.group/tombishop/32/23_2.png) [@tombishop](https://datashield.discourse.group/u/tombishop)\
**Post date:** [19 May 2021 08:56 UTC](https://datashield.discourse.group/t/creating-dummy-variables/396/6 "2021-05-19T08:56:25Z")

</div>

Thanks again!

Actually I just found another way, which is to use `ds.matrixDimnames` on the output from `ds.asFactor` (which is conveniently a matrix)
