-2

This should be fairly simple, not sure why I can't find the answer.

I have a dataset with 15 variables (columns) from a csv. 2 variables are called Cancer and copyofCancer, indicating whether or not someone has cancer. Those 2 columns should become 1 new column where column copyofCancer is appended to Cancer.

When combining the 2 columns into 1 new column, I should have the new column added in the complete dataset of 15 variables. I found solutions where I had a dataframe with only 3 variables (Cancer, copyofCancer, Cancercombined) but could not find a way to have this added to my complete dataset of 15 variables. My dataset is called risk

For example

Cancer:  yes no yes no
copyofCancer: no no yes no

becomes column:

Cancercombined: yes no yes no no no yes no
Kirsten
  • 37
  • 1
  • 6
  • Not clear regarding the input format and expected output. Do you have a (named) vector? A `data.frame`? Perhaps you are after `cbind()`? Or `c()`? PS. Not my downvote. – Maurits Evers Nov 28 '19 at 20:50
  • 3
    @Kirsten Welcome to SO! Please read [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Following the guidelines will make it much easier for others to help you. – markus Nov 28 '19 at 20:51
  • I think you need `c(t(data))` or `unlist(data)` It – akrun Nov 28 '19 at 21:05
  • @akrun Thanks for the answers. In c(t(data) or unlist(data) how do I refer to the columns that need to be merged. And I need to create a new column. So c(t(risk) -> risk dataset has columns Cancer and Copyofcancer – Kirsten Nov 28 '19 at 21:15
  • It is a vector. So, you can just wrap `data.frame(newcol = unlist(data))` – akrun Nov 28 '19 at 21:17
  • You can combine the two columns by doing `c(risk$Cancer, risk$copyofCancer)` but you cannot add it to your dataframe since now the length is doubled. – Ronak Shah Nov 29 '19 at 06:31
  • @RonakShah Oh thanks for your clear answer. Now I understand the problem, I was earlier indeed able combine the 2 columns with c but not add it to the dataset. – Kirsten Nov 30 '19 at 09:30
  • @RonakShah will do – Kirsten Nov 30 '19 at 10:01

1 Answers1

1

You can combine the two columns by using c or append i.e

c(risk$Cancer, risk$copyofCancer)

Or

append(risk$Cancer, risk$copyofCancer)

However, since this is combination of two columns length of it has been doubled so you cannot add it to the dataframe.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213