Home > Software design >  Convert column value NN to numbers in R
Convert column value NN to numbers in R

Time:11-20

    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!

CodePudding user response:

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']

CodePudding user response:

Maybe we can try this

x <- c("NN", "NaN", "NI", "II")
v <- c(N = "0", Na = "..", I = "1")
p <- setNames(
  c(outer(v, v, Vectorize(function(x, y) paste(x, y, sep = "/")))),
  c(outer(names(v), names(v), paste0))
)

then

> p[x]
    NN    NaN     NI     II 
 "0/0" "../0"  "0/1"  "1/1"

CodePudding user response:

If it was one character to one character change, we could simply use chartr (character translate), but as we have "Na" to "..", I am converting "Na" to a single character, then adding "/" after 1st character, then replacing "A" into "..", finally translating NI to 01.

x <- c("NN","NaN","NI","II")
x1 <- gsub("Na","A", x)

chartr("NI", "01", gsub("A", "..", paste(substr(x1, 1, 1), substr(x1, 2, 2), sep = "/")))
# [1] "0/0"  "../0" "0/1"  "1/1" 

CodePudding user response:

Using str_replace:

library(dplyr)
library(stringr)

mydata <- data.frame(
  col_1 = c("NN", "NaN", "NI", "II")
)

mydata %>% 
  mutate(col_2 = str_replace(col_1, "Na", "../"), 
         col_2 = str_replace(col_2, "N", "0/"),
         col_2 = str_replace_all(col_2, "I", "1/"),
         col_2 = str_replace(col_2, "(.$)", ""))

CodePudding user response:

You can use str_replace_all from the stringr package:

library(stringr)

df$col2 <- str_replace_all(df$col_1, c("Na" = "../", "N" = "0/", "I" = "1/")) %>% 
  str_remove(".$")
df
#>   col_1 col2
#> 1    NN  0/0
#> 2   NaN ../0
#> 3    NI  0/1
#> 4    II  1/1

You supply a vector of replacements and then you add a / before the last number. Note that the presence of Na is checked before N, so to have a correct replacement.

Where df is:

df <- read.table(text = "col_1
    NN
    NaN
    NI
    II", header = TRUE)

CodePudding user response:

x <- c("NN", "NaN", "NI", "II", "NNa")
v <- c("Na" = "..", "N" = "0", "I" = "1")
p <- lapply(stringr::str_extract_all(x, "(Na|N|I)"), function(x) paste(stringr::str_replace_all(x, v), collapse = "/"))

p

# [[1]]
# [1] "0/0"
# 
# [[2]]
# [1] "../0"
# 
# [[3]]
# [1] "0/1"
# 
# [[4]]
# [1] "1/1"
# 
# [[5]]
# [1] "0/.."
  •  Tags:  
  • r
  • Related