-1

I have the columns "male" and "female" in a data.frame. The rows of these columns are numbers.

To make it more tidy, I would like to add a new column "gender" for male/female and right next to it, I want to combine the both existing columns "male" and "female" (with the numbers in it).

How can I do that? I tried bind_rows() and mutate() but failed.

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
Alice Wood
  • 103
  • 1
  • 1
    Try with `gather` or `melt` – akrun May 10 '18 at 17:57
  • ... otherwise you'll need to make this question more [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including sample data (such as `dput(head(x))`), code you've tried so far, and your expected output. – r2evans May 10 '18 at 17:59
  • Always try to add reproducible data to your questions" XX – Giovana Stein May 10 '18 at 18:35

1 Answers1

-1

If you are using bind_rows and mutate, it sounds like you use dplyr in the tidyverse, so I would use the tidyr package to do this

library(tidyr)
df <- data.frame(Text = c("Foo","Bar","babel"),Male =c(1,2,3), Female = c(4,5,6), stringsAsFactors = FALSE)

gather(df, Gender, GenderValue, Male, Female)
Kerry Jackson
  • 1,821
  • 12
  • 20