Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Problem removing whitespace from a string in r

$
0
0

I know this seems like a duplicate, but I have tried several solutions to this problem including this one, maybe I have a problem with my scraping that produces this problem

Example

This is what I am trying to do:

I scraped this dataset from Wikipedia:

library(tidyverse)
library(rvest)

Sueldos <- read_html("https://es.wikipedia.org/wiki/Anexo:Salario_m%C3%ADnimo_en_Chile") %>% html_nodes(".wikitable") 

Sueldos <-rvest::html_table(Sueldos[[1]])

colnames(Sueldos) <- make.names(colnames(Sueldos))

Sueldos <- Sueldos %>% rename(Sueldo = Monto.bruto.enpesos.chilenos) %>% dplyr::select(Desde, Sueldo)

The dataset looks like this:

|Desde                   |Sueldo |
|:-----------------------|:------|
|1 de septiembre de 1987 |11 335 |
|1 de febrero de 1989    |15 488 |
|1 de junio de 1989      |18 000 |
|1 de junio de 1990      |26 000 |
|1 de junio de 1991      |33 000 |
|1 de junio de 1992      |38 600 |
|1 de junio de 1993      |46 000 |
|1 de junio de 1994      |52 150 |
|1 de junio de 1995      |58 900 |
|1 de junio de 1996      |65 500 |

Usually, I have no problem trimming this whitespace, but several trials have given me the same result:

Test 1

I first thought I would try with str_remove_all from stringr

Sueldos <- Sueldos %>%  str_remove_all(Sueldo, pattern = "")

But it didn't change

Test 2

Then I thought of using str_replace_all

Sueldos <- Sueldos %>%  mutate(Sueldo = str_replace_all(Sueldo, pattern = "", replacement = ""))

And nothing

Test 3

I thought maybe as in the link above if I used fixed it would work

Sueldos <- Sueldos %>%  mutate(Sueldo = str_replace_all(Sueldo, pattern = fixed(""), replacement = ""))

Nothing

Test 4

I finally tried gsub, just in case

Sueldos$Sueldo <- gsub(x =  Sueldos$Sueldo, pattern = "", replacement = "")

Still nothing, I dont get what is going on

SessionInfo:

R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=es_CL.UTF-8        LC_COLLATE=en_US.UTF-8    
  [5] LC_MONETARY=es_CL.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=es_CL.UTF-8       LC_NAME=C                 
  [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=es_CL.UTF-8 LC_IDENTIFICATION=C       

 attached base packages:
 [1] stats     graphics  grDevices utils     datasets  methods   base     

 other attached packages:
  [1] forcats_0.4.0   stringr_1.4.0   dplyr_0.8.3     purrr_0.3.3     readr_1.3.1     tidyr_1.0.0     tibble_2.1.3   
  [8] ggplot2_3.2.1   tidyverse_1.2.1 rvest_0.3.4     xml2_1.2.2     

 loaded via a namespace (and not attached):
  [1] tidyselect_0.2.5  xfun_0.10         haven_2.1.1       lattice_0.20-38   colorspace_1.4-1  vctrs_0.2.0       generics_0.0.2   
  [8] htmltools_0.4.0   yaml_2.2.0        rlang_0.4.1       pillar_1.4.2      glue_1.3.1        withr_2.1.2       selectr_0.4-1    
 [15] modelr_0.1.5      readxl_1.3.1      lifecycle_0.1.0   munsell_0.5.0     gtable_0.3.0      cellranger_1.1.0  htmlwidgets_1.5.1
 [22] evaluate_0.14     knitr_1.25        curl_4.2          highr_0.8         htmlTable_1.13.2  broom_0.5.2       Rcpp_1.0.2       
 [29] scales_1.0.0      backports_1.1.5   checkmate_1.9.4   jsonlite_1.6      hms_0.5.1         digest_0.6.22     stringi_1.4.3    
 [36] grid_3.6.1        cli_1.1.0         tools_3.6.1       magrittr_1.5      lazyeval_0.2.2    crayon_1.3.4      pkgconfig_2.0.3  
 [43] zeallot_0.1.0     lubridate_1.7.4   assertthat_0.2.1  rmarkdown_1.16    httr_1.4.1        rstudioapi_0.10   R6_2.4.0         
 [50] nlme_3.1-141      compiler_3.6.1   

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>