Search code examples
rif-statementsubstr

ifelse for different locations in a string


I would like help with creating a categorial value (called x2) using an ifelse statement on string variable x1. Or something else than 'ifelse' of course, but I think it should work with 'ifelse' ?

image of dataframe

dataframe typed out:
patientnumber <- c('1', '2', '3')
x1 <- c('54836', '12789', '78955')

the class from x1 is 'character'. The characters in x1 have multiple meanings.

if the first character in x1 == 1, then I want a new variable x2 to become 'yes', if not, 'no'. What I did was : create a new column(called x1.1) using substr and then use the ifelse statement on that new column, and that works.

data$x1.1 <- substr(data$x1, 1, 1) #create x1.1 by taking from x1 the first char, and use 1 char in total

data$x2 <- ifelse(data$x1.1 == 1, 'yes', 'no')

image of dataframe with added x1.1 and x2

But I am wondering if I can do it shorter. So get x2 without having to need to create x1.1. I had something like this in mind, but it does not work:

data$x2 <- ifelse(data$x1[ 1 ] == 1, 'yes', 'no') # so I instead of 'x1', i call 'x1[ 1 ]' so this is what I want the code to do:

for patient 1, if the first char in x1 == 1, the outcome in new column x2 == 'yes', if not, then 'no'

for patient 2, if the first char in x1 == 1, the outcome in new column x2 == 'yes', if not, then 'no'

for patient 3, if the first char in x1 == 1, the outcome in new column x2 == 'yes', if not, then 'no'

Moreover, rather than looking at the first character, I would also like to be able to:

-look at the second character

-look at the second+third character together

There is a way for me to do it, but instead of just one x1, i actually have x1 , x2 x3....x29 x30. And within them there are 4 meanings, so doing it 'my way' it would mean I first have to create 120 new columns first before I could even start with creating the correct corresponding categorial columns. It makes my dataframe bigger and gives a lot of code lines I would like to prevent.

I hope this community can help me out. Cheers :)

P.S. This is my first post on stackoverflow, if you have tips on how to make my post better, please let me know (i already looked at some youtube videos but they did not help, my biggest issue nog is how to show a dataframe in a nice way)

P.S. I used the search function but could not find an answer to this question.


Solution

  • Try this:

    data$x2 <- ifelse(substr(x1, 1, 1) == "1", "yes", "no")
    

    Maybe also take a look at dplyr::case_when if you need several ifelse statements.