Search code examples
rstringtidyversedplyrrecode

Replacing tick mark √ with 1


I have data where some students have checked by symbol √ in Excel file. I need to replace it with 1 (one) in my column(s). I have 50 values in a column where this symbol occurs. How can I do this using dplyr? or some other R function?


Solution

  • You could use str_replace from stringr:

    library(dplyr)
    library(stringr)
    data
      A B
    1 1 √
    2 2 2
    3 3 √
    4 4 √
    5 5 2
    
    data %>%
      mutate(B = str_replace(B,"√","1"))
      A B
    1 1 1
    2 2 2
    3 3 1
    4 4 1
    5 5 2
    

    You could add as.numeric if you want to also convert to numeric:

    data %>%
      mutate(B = as.numeric(str_replace(B,"√","1")))
    

    Sample Data

    set.seed(3)
    data <- data.frame(A= 1:5,B = gsub("1","√",round(runif(5,1,2))))