0

I have a column with multiple response values. Answers look like "1,2,3" and "1,4,5" for example. I have successfully created binary columns that tell me if a number is present in the character string listed in the column.

Right now: I have 0 values for the entire subsequent columns when there is no response in my main column.

I want there to be all NA values when there is nothing in my main column. My code right now changes all the values in the subsequent column to NA.

Here is my code right now:

if(data$main== "") {
data[c(which(colnames(data)=="One"):which(colnames(data)=="Five"))] = NA
}
blaze
  • 57
  • 1
  • 3
  • 3
    Hi @blaze, you question would benefit from a reproducible example, see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Bulat Sep 17 '19 at 22:33
  • You also have not asked a question since you do not say what is wrong with the code you have so far. One approach would be to create five columns of NAs and then replace them with 1s and 0s when the main column is not empty. – dcarlson Sep 17 '19 at 23:09
  • Try `splitstackshape::cSplit_e(data, "main", type = "character", fill = 0)` See https://stackoverflow.com/questions/39461539/convert-column-with-pipe-delimited-data-into-dummy-variables/39461990#39461990 – Ronak Shah Sep 18 '19 at 00:54
  • Hi Blaze, yes, please edit your question and provide us with your data or a subset of it (perhaps using `dput`) so we can cut and paste your code and get same bad response where values in subsequent column and turning to NA. Thanks :) – mysteRious Sep 19 '19 at 01:45

0 Answers0