Mutate when quote marks within character string

Hi there,

I'm having trouble renaming variables within a column (I think) because quotation marks are used within the variable.

I want to change "I prefer a 9.9% return on the socially responsible portfolio focusing on "gender diversity" ($990)" to "$10" in the Value column.

Here is some data and the code I have tried

dat<-data.frame(
  stringsAsFactors = FALSE,
                        Question = c("G2Q00006","G2Q00007","G2Q00008","G2Q00009",
                                     "G2Q00010","G2Q00011","G2Q00012",
                                     "G2Q00013","G2Q00014","G2Q00015"),
                 n = c(41L, 33L, 19L, 11L, 7L, 3L, 1L, 2L, 3L, 1L),
                          Stream = c("gender","gender","gender","gender",
                                     "gender","gender","gender","gender","gender",
                                     "gender"),
                           Value = as.factor(c("I prefer a 9.9% return on the socially responsible portfolio focusing on \"gender diversity\" ($990)",
                                               "I prefer a 9% return on the socially responsible portfolio focusing on \"gender diversity\" ($900)",
                                               "I prefer a 8% return on the socially responsible portfolio focusing on \"gender diversity\" ($800)",
                                               "I prefer a 7% return on the socially responsible portfolio focusing on \"gender diversity\" ($700)",
                                               "I prefer a 6% return on the socially responsible portfolio focusing on \"gender diversity\" ($600)",
                                               "I prefer a 5% return on the socially responsible portfolio focusing on \"gender diversity\" ($500)",
                                               "I prefer a 4% return on the socially responsible portfolio focusing on \"gender diversity\" ($400)",
                                               "I prefer a 3% return on the socially responsible portfolio focusing on \"gender diversity\" ($300)",
                                               "I prefer a 2% return on the socially responsible portfolio focusing on \"gender diversity\" ($200)",
                                               "I prefer a 1% return on the socially responsible portfolio focusing on \"gender diversity\" ($100)"))
              )

dat <- dat %>%
  mutate(Value = recode(Value, 
                       `I prefer a 9.9% return on the socially responsible portfolio focusing on \"gender diversity\" ($990)` = "$10"))
#> Error in dat %>% mutate(Value = recode(Value, `I prefer a 9.9% return on the socially responsible portfolio focusing on "gender diversity" ($990)` = "$10")): could not find function "%>%"

Created on 2023-10-31 with reprex v2.0.2

I have also tried the same code with quotation marks

dat<-data.frame(
  stringsAsFactors = FALSE,
                        Question = c("G2Q00006","G2Q00007","G2Q00008","G2Q00009",
                                     "G2Q00010","G2Q00011","G2Q00012",
                                     "G2Q00013","G2Q00014","G2Q00015"),
                 n = c(41L, 33L, 19L, 11L, 7L, 3L, 1L, 2L, 3L, 1L),
                          Stream = c("gender","gender","gender","gender",
                                     "gender","gender","gender","gender","gender",
                                     "gender"),
                           Value = as.factor(c("I prefer a 9.9% return on the socially responsible portfolio focusing on \"gender diversity\" ($990)",
                                               "I prefer a 9% return on the socially responsible portfolio focusing on \"gender diversity\" ($900)",
                                               "I prefer a 8% return on the socially responsible portfolio focusing on \"gender diversity\" ($800)",
                                               "I prefer a 7% return on the socially responsible portfolio focusing on \"gender diversity\" ($700)",
                                               "I prefer a 6% return on the socially responsible portfolio focusing on \"gender diversity\" ($600)",
                                               "I prefer a 5% return on the socially responsible portfolio focusing on \"gender diversity\" ($500)",
                                               "I prefer a 4% return on the socially responsible portfolio focusing on \"gender diversity\" ($400)",
                                               "I prefer a 3% return on the socially responsible portfolio focusing on \"gender diversity\" ($300)",
                                               "I prefer a 2% return on the socially responsible portfolio focusing on \"gender diversity\" ($200)",
                                               "I prefer a 1% return on the socially responsible portfolio focusing on \"gender diversity\" ($100)"))
              )


dat$Value[dat$Value=="I prefer a 9.9% return on the socially responsible portfolio focusing on \"gender diversity\" ($990)"] <- "$10"
#> Warning in `[<-.factor`(`*tmp*`, dat$Value == "I prefer a 9.9% return on the
#> socially responsible portfolio focusing on \"gender diversity\" ($990)", :
#> invalid factor level, NA generated

Created on 2023-10-31 with reprex v2.0.2

Any help or advice would be much appreciated.

Is this the output that you're expecting?

> dat
   Question  n Stream                                                                                            Value
1  G2Q00006 41 gender                                                                                              $10
2  G2Q00007 33 gender I prefer a 9% return on the socially responsible portfolio focusing on "gender diversity" ($900)
3  G2Q00008 19 gender I prefer a 8% return on the socially responsible portfolio focusing on "gender diversity" ($800)
4  G2Q00009 11 gender I prefer a 7% return on the socially responsible portfolio focusing on "gender diversity" ($700)
5  G2Q00010  7 gender I prefer a 6% return on the socially responsible portfolio focusing on "gender diversity" ($600)
6  G2Q00011  3 gender I prefer a 5% return on the socially responsible portfolio focusing on "gender diversity" ($500)
7  G2Q00012  1 gender I prefer a 4% return on the socially responsible portfolio focusing on "gender diversity" ($400)
8  G2Q00013  2 gender I prefer a 3% return on the socially responsible portfolio focusing on "gender diversity" ($300)
9  G2Q00014  3 gender I prefer a 2% return on the socially responsible portfolio focusing on "gender diversity" ($200)
10 G2Q00015  1 gender I prefer a 1% return on the socially responsible portfolio focusing on "gender diversity" ($100)

Your code at the top was fine for me:

R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252    LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                       LC_TIME=English_Australia.1252    

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

other attached packages:
[1] dplyr_1.1.3

Anyway the error in your first bit of code will be fixed by newer versions of dplyr or by loading the tidyverse or magrittr.

Hi @williaml - thank you for your prompt response.

Yes, that is the output I am expecting. I tried loading tidyverse and magrittr, but I still encountered the same issue...

dat<-data.frame(
  stringsAsFactors = FALSE,
                        Question = c("G2Q00006","G2Q00007","G2Q00008","G2Q00009",
                                     "G2Q00010","G2Q00011","G2Q00012",
                                     "G2Q00013","G2Q00014","G2Q00015"),
                 n = c(41L, 33L, 19L, 11L, 7L, 3L, 1L, 2L, 3L, 1L),
                          Stream = c("gender","gender","gender","gender",
                                     "gender","gender","gender","gender","gender",
                                     "gender"),
                           Value = as.factor(c("I prefer a 9.9% return on the socially responsible portfolio focusing on \"gender diversity\" ($990)",
                                               "I prefer a 9% return on the socially responsible portfolio focusing on \"gender diversity\" ($900)",
                                               "I prefer a 8% return on the socially responsible portfolio focusing on \"gender diversity\" ($800)",
                                               "I prefer a 7% return on the socially responsible portfolio focusing on \"gender diversity\" ($700)",
                                               "I prefer a 6% return on the socially responsible portfolio focusing on \"gender diversity\" ($600)",
                                               "I prefer a 5% return on the socially responsible portfolio focusing on \"gender diversity\" ($500)",
                                               "I prefer a 4% return on the socially responsible portfolio focusing on \"gender diversity\" ($400)",
                                               "I prefer a 3% return on the socially responsible portfolio focusing on \"gender diversity\" ($300)",
                                               "I prefer a 2% return on the socially responsible portfolio focusing on \"gender diversity\" ($200)",
                                               "I prefer a 1% return on the socially responsible portfolio focusing on \"gender diversity\" ($100)"))
              )


dat <- dat %>%
  mutate(Value = recode(Value, 
                       `I prefer a 9.9% return on the socially responsible portfolio focusing on \"gender diversity\" ($990)` = "$10"))
#> Error in dat %>% mutate(Value = recode(Value, `I prefer a 9.9% return on the socially responsible portfolio focusing on "gender diversity" ($990)` = "$10")): could not find function "%>%"
dat
#>    Question  n Stream
#> 1  G2Q00006 41 gender
#> 2  G2Q00007 33 gender
#> 3  G2Q00008 19 gender
#> 4  G2Q00009 11 gender
#> 5  G2Q00010  7 gender
#> 6  G2Q00011  3 gender
#> 7  G2Q00012  1 gender
#> 8  G2Q00013  2 gender
#> 9  G2Q00014  3 gender
#> 10 G2Q00015  1 gender
#>                                                                                                 Value
#> 1  I prefer a 9.9% return on the socially responsible portfolio focusing on "gender diversity" ($990)
#> 2    I prefer a 9% return on the socially responsible portfolio focusing on "gender diversity" ($900)
#> 3    I prefer a 8% return on the socially responsible portfolio focusing on "gender diversity" ($800)
#> 4    I prefer a 7% return on the socially responsible portfolio focusing on "gender diversity" ($700)
#> 5    I prefer a 6% return on the socially responsible portfolio focusing on "gender diversity" ($600)
#> 6    I prefer a 5% return on the socially responsible portfolio focusing on "gender diversity" ($500)
#> 7    I prefer a 4% return on the socially responsible portfolio focusing on "gender diversity" ($400)
#> 8    I prefer a 3% return on the socially responsible portfolio focusing on "gender diversity" ($300)
#> 9    I prefer a 2% return on the socially responsible portfolio focusing on "gender diversity" ($200)
#> 10   I prefer a 1% return on the socially responsible portfolio focusing on "gender diversity" ($100)

Created on 2023-10-31 with reprex v2.0.2

These are my system details

R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.6

Matrix products: default

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
 [1] reprex_2.0.2    lubridate_1.9.3 forcats_1.0.0   stringr_1.5.0   dplyr_1.1.3     purrr_1.0.2    
 [7] readr_2.1.4     tidyr_1.3.0     tibble_3.2.1    ggplot2_3.4.4   tidyverse_2.0.0 magrittr_2.0.3 

loaded via a namespace (and not attached):
 [1] gtable_0.3.4     compiler_4.3.1   tidyselect_1.2.0 clipr_0.8.0      callr_3.7.3     
 [6] scales_1.2.1     yaml_2.3.7       fastmap_1.1.1    R6_2.5.1         generics_0.1.3  
[11] knitr_1.43       munsell_0.5.0    pillar_1.9.0     tzdb_0.4.0       rlang_1.1.1     
[16] utf8_1.2.4       stringi_1.7.12   xfun_0.39        fs_1.6.2         timechange_0.2.0
[21] cli_3.6.1        withr_2.5.1      ps_1.7.5         processx_3.8.2   digest_0.6.31   
[26] grid_4.3.1       rstudioapi_0.14  hms_1.1.3        lifecycle_1.0.3  vctrs_0.6.4     
[31] evaluate_0.21    glue_1.6.2       fansi_1.0.5      colorspace_2.1-0 rmarkdown_2.22  
[36] htmltools_0.5.5  tools_4.3.1      pkgconfig_2.0.3 

I'm running the same version of dplyr so not sure why I'm not getting the correct output...

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.