Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert column value NN to numbers in R

Tags:

r

    col_1
    NN
    NaN
    NI
    II

How do I change it to:

    col_2
    0/0
    ../0
    0/1
    1/1

where N=0, Na=.., I=1, with / in between the two numbers?

I tried using for loops but it was a mess and I dont know how to add the / in the middle... Thank you so much in advance!

like image 645
beepboopbeep Avatar asked Dec 09 '22 23:12

beepboopbeep


1 Answers

You can try to apply following function. It uses regex expressions and substitution.

adjust_text <- function(text){
  text <- gsub('Na', '../', text)
  text <- gsub('N', '0/', text)
  text <- gsub('I', '1/', text)
  text <- gsub('(.$)', '', text)
  return(text)}

Sample output:

['0/0', '../0', '0/1', '1/1']
like image 122
Slybot Avatar answered Dec 29 '22 21:12

Slybot