removing particular character in a column in r

后端 未结 3 387
不思量自难忘°
不思量自难忘° 2020-12-06 07:01

I have a table called LOAN containing column named RATE in which the observations are given in percentage for example 14.49% how can i format the table so that all value in

3条回答
  •  一向
    一向 (楼主)
    2020-12-06 07:26

    This can be achieved using the mutate verb from the tidyverse package. Which in my opinion is more readable. So, to exemplify this, I create a dataset called LOAN with a focus on the RATE to mimic the problem above.

    library(tidyverse)
    LOAN <- data.frame("SN" = 1:4, "Age" = c(21,47,68,33), 
                       "Name" = c("John", "Dora", "Ali", "Marvin"),
                       "RATE" = c('16%', "24.5%", "27.81%", "22.11%"), 
                       stringsAsFactors = FALSE)
    head(LOAN)
      SN Age   Name   RATE
    1  1  21   John    16%
    2  2  47   Dora  24.5%
    3  3  68    Ali 27.81%
    4  4  33 Marvin 22.11%
    

    In what follows, mutate allows one to alter the column content, gsub does the desired substitution (of % with "") and converts the RATE column to numeric value, keeping the data cleaning flow followable.

    LOAN <- LOAN %>% mutate(RATE = as.numeric(gsub("%", "", RATE)))
    head(LOAN)
      SN Age   Name  RATE
    1  1  21   John 16.00
    2  2  47   Dora 24.50
    3  3  68    Ali 27.81
    4  4  33 Marvin 22.11
    

提交回复
热议问题