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.