Problem getting stringr replace_all to work

Hi there,

I am trying to tidy up some data for sentiment analysis. Specifically, in the 'description of the call' column there are a lot of unnecessary spaces and the occasional '#'.

Because I am trying to edit the content within the column I thought I had to use 'mutate' but I get the following error Error in mutate(): ! Problem while computing ..1 = ~.... ✖ ..1must be a vector, not aformula object.

Here is my code (reprex):

library(tidytext)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(stringr)
library(reprex)

sa <- read.csv("reprex_call_data.csv")
#> Warning in file(file, "rt"): cannot open file 'reprex_call_data.csv': No such
#> file or directory
#> Error in file(file, "rt"): cannot open the connection
head(sa, n=20)
#> Error in head(sa, n = 20): object 'sa' not found

sa <- sa %>%
  mutate(
    ~stringr::str_replace_all(.x, "\\#", " ") %>%
    stringr::str_squish() %>%
    stringr::str_replace_all("\\n" = " "))
#> Error in mutate(., ~stringr::str_replace_all(.x, "\\#", " ") %>% stringr::str_squish() %>% : object 'sa' not found
Created on 2022-10-26 with reprex v2.0.2

Thank you for your assistance

The problem seems to be

read.csv("reprex_call_data.csv")

Is sa loaded correctly?

Thanks @Flm - yes, sa loads correctly. The first 9 observations are blank but the 10th brings up the following text under 'description of the call' column in the console window:

10 Surrounded affronting favourable no mr. Lain knew like half she yet joy. Be than dull as seen very shot. Attachment ye so am travelling estimating projecting is. Off fat address attacks his besides. Suitable settling mr attended no doubtful feelings. Any over for say bore such sold five but hung.\n\nDemesne far hearted suppose venture excited see had has. Dependent on so extremely delivered by. Yet no jokes worse her why. Bed one supposing breakfast day fulfilled off depending questions. Whatever boy her exertion his extended. Ecstatic followed handsome drawings entirely mrs one yet outweigh. Of acceptance insipidity remarkably is invitation.\n\nFull he none no side. Uncommonly surrounded considered for him are its. It we is read good soon. My to considered delightful invitation announcing of no decisively boisterous. Did add dashwoods deficient man concluded additions resources. Or landlord packages overcame distance smallest in recurred. Wrong maids or be asked no on enjoy. Household few sometimes out attending described. Lain just fact four of am meet high.\n\nCan curiosity may end shameless explained. True high on said mr on come. An do mr design at little myself wholly entire though. Attended of on stronger or mr pleasure. Rich four like real yet west get. Felicity in dwelling to drawings. His pleasure new steepest for reserved formerly disposed jennings.

Can you put a reproducible example using dput(head(sa, 15))

Thanks @Flm - apologies if this is in the wrong format but when I run the code provided this is what I get:

structure(list(ID = 1:10, Location = c("Gold Coast", "Gold Coast", 
"Gold Coast", "Gold Coast", "Western Qld", "Darling Downs South West", 
"Sunshine Coast", "Gold Coast", "Gold Coast", "Sunshine Coast"
), Uncontactable.reason = c("Left message for them to call back if interested in talking", 
"Left message for them to call back if interested in talking", 
"Left message for them to call back if interested in talking", 
"Left message for them to call back if interested in talking", 
"Left message for them to call back if interested in talking", 
"Left message for them to call back if interested in talking", 
"Left message for them to call back if interested in talking", 
"Left message for them to call back if interested in talking", 
"Left message for them to call back if interested in talking", 
""), Date.of.call = c("2021/03/05 16:30:14", "2021/03/05 16:29:42", 
"2021/03/05 16:29:04", "2021/03/05 16:27:01", "2021/03/05 16:26:32", 
"2021/03/05 16:25:54", "2021/03/05 16:25:24", "2021/03/05 16:24:48", 
"2021/03/05 16:23:40", "2021/03/02"), Description.of.the.call = c("", 
"", "", "", "", "", "", "", "", "Surrounded affronting favourable no mr. Lain knew like half she yet joy. Be than dull as seen very shot. Attachment ye so am travelling estimating projecting is. Off fat address attacks his besides. Suitable settling mr attended no doubtful feelings. Any over for say bore such sold five but hung.\n\nDemesne far hearted suppose venture excited see had has. Dependent on so extremely delivered by. Yet no jokes worse her why. Bed one supposing breakfast day fulfilled off depending questions. Whatever boy her exertion his extended. Ecstatic followed handsome drawings entirely mrs one yet outweigh. Of acceptance insipidity remarkably is invitation.\n\nFull he none no side. Uncommonly surrounded considered for him are its. It we is read good soon. My to considered delightful invitation announcing of no decisively boisterous. Did add dashwoods deficient man concluded additions resources. Or landlord packages overcame distance smallest in recurred. Wrong maids or be asked no on enjoy. Household few sometimes out attending described. Lain just fact four of am meet high.\n\nCan curiosity may end shameless explained. True high on said mr on come. An do mr design at little myself wholly entire though. Attended of on stronger or mr pleasure. Rich four like real yet west get. Felicity in dwelling to drawings. His pleasure new steepest for reserved formerly disposed jennings."
), Areas.of.concern = c("", "", "", "", "", "", "", "", "", "Lorem; Ipsum;"
)), row.names = c(NA, 10L), class = "data.frame")

The mocked up dataset can also be downloaded at Dropbox - reprex_call_data.csv - Simplify your life, if that's easier.

Thanks for looking into this.

Is this what you are trying to do ?

library(tidyverse)

# Sample data in a copy/paste friendly format
sa <- structure(list(ID = 1:10, Location = c("Gold Coast", "Gold Coast", 
                                                    "Gold Coast", "Gold Coast", "Western Qld", "Darling Downs South West", 
                                                    "Sunshine Coast", "Gold Coast", "Gold Coast", "Sunshine Coast"
), Uncontactable.reason = c("Left message for them to call back if interested in talking", 
                            "Left message for them to call back if interested in talking", 
                            "Left message for them to call back if interested in talking", 
                            "Left message for them to call back if interested in talking", 
                            "Left message for them to call back if interested in talking", 
                            "Left message for them to call back if interested in talking", 
                            "Left message for them to call back if interested in talking", 
                            "Left message for them to call back if interested in talking", 
                            "Left message for them to call back if interested in talking", 
                            ""), Date.of.call = c("2021/03/05 16:30:14", "2021/03/05 16:29:42", 
                                                  "2021/03/05 16:29:04", "2021/03/05 16:27:01", "2021/03/05 16:26:32", 
                                                  "2021/03/05 16:25:54", "2021/03/05 16:25:24", "2021/03/05 16:24:48", 
                                                  "2021/03/05 16:23:40", "2021/03/02"), Description.of.the.call = c("", 
                                                                                                                    "", "", "", "", "", "", "", "", "Surrounded affronting favourable no mr. Lain knew like half she yet joy. Be than dull as seen very shot. Attachment ye so am travelling estimating projecting is. Off fat address attacks his besides. Suitable settling mr attended no doubtful feelings. Any over for say bore such sold five but hung.\n\nDemesne far hearted suppose venture excited see had has. Dependent on so extremely delivered by. Yet no jokes worse her why. Bed one supposing breakfast day fulfilled off depending questions. Whatever boy her exertion his extended. Ecstatic followed handsome drawings entirely mrs one yet outweigh. Of acceptance insipidity remarkably is invitation.\n\nFull he none no side. Uncommonly surrounded considered for him are its. It we is read good soon. My to considered delightful invitation announcing of no decisively boisterous. Did add dashwoods deficient man concluded additions resources. Or landlord packages overcame distance smallest in recurred. Wrong maids or be asked no on enjoy. Household few sometimes out attending described. Lain just fact four of am meet high.\n\nCan curiosity may end shameless explained. True high on said mr on come. An do mr design at little myself wholly entire though. Attended of on stronger or mr pleasure. Rich four like real yet west get. Felicity in dwelling to drawings. His pleasure new steepest for reserved formerly disposed jennings."
                                                  ), Areas.of.concern = c("", "", "", "", "", "", "", "", "", "Lorem; Ipsum;"
                                                  )), row.names = c(NA, 10L), class = "data.frame")

# Relevant code
sa %>%
    mutate(Description.of.the.call = str_replace_all(Description.of.the.call, "\\#", "") %>% 
               str_squish() %>% 
               str_replace_all("\\n", ""))
#>    ID                 Location
#> 1   1               Gold Coast
#> 2   2               Gold Coast
#> 3   3               Gold Coast
#> 4   4               Gold Coast
#> 5   5              Western Qld
#> 6   6 Darling Downs South West
#> 7   7           Sunshine Coast
#> 8   8               Gold Coast
#> 9   9               Gold Coast
#> 10 10           Sunshine Coast
#>                                           Uncontactable.reason
#> 1  Left message for them to call back if interested in talking
#> 2  Left message for them to call back if interested in talking
#> 3  Left message for them to call back if interested in talking
#> 4  Left message for them to call back if interested in talking
#> 5  Left message for them to call back if interested in talking
#> 6  Left message for them to call back if interested in talking
#> 7  Left message for them to call back if interested in talking
#> 8  Left message for them to call back if interested in talking
#> 9  Left message for them to call back if interested in talking
#> 10                                                            
#>           Date.of.call
#> 1  2021/03/05 16:30:14
#> 2  2021/03/05 16:29:42
#> 3  2021/03/05 16:29:04
#> 4  2021/03/05 16:27:01
#> 5  2021/03/05 16:26:32
#> 6  2021/03/05 16:25:54
#> 7  2021/03/05 16:25:24
#> 8  2021/03/05 16:24:48
#> 9  2021/03/05 16:23:40
#> 10          2021/03/02
#>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          Description.of.the.call
#> 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
#> 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
#> 3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
#> 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
#> 5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
#> 6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
#> 7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
#> 8                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
#> 9                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
#> 10 Surrounded affronting favourable no mr. Lain knew like half she yet joy. Be than dull as seen very shot. Attachment ye so am travelling estimating projecting is. Off fat address attacks his besides. Suitable settling mr attended no doubtful feelings. Any over for say bore such sold five but hung. Demesne far hearted suppose venture excited see had has. Dependent on so extremely delivered by. Yet no jokes worse her why. Bed one supposing breakfast day fulfilled off depending questions. Whatever boy her exertion his extended. Ecstatic followed handsome drawings entirely mrs one yet outweigh. Of acceptance insipidity remarkably is invitation. Full he none no side. Uncommonly surrounded considered for him are its. It we is read good soon. My to considered delightful invitation announcing of no decisively boisterous. Did add dashwoods deficient man concluded additions resources. Or landlord packages overcame distance smallest in recurred. Wrong maids or be asked no on enjoy. Household few sometimes out attending described. Lain just fact four of am meet high. Can curiosity may end shameless explained. True high on said mr on come. An do mr design at little myself wholly entire though. Attended of on stronger or mr pleasure. Rich four like real yet west get. Felicity in dwelling to drawings. His pleasure new steepest for reserved formerly disposed jennings.
#>    Areas.of.concern
#> 1                  
#> 2                  
#> 3                  
#> 4                  
#> 5                  
#> 6                  
#> 7                  
#> 8                  
#> 9                  
#> 10    Lorem; Ipsum;

Created on 2022-10-26 with reprex v2.0.2

BTW, a reprex requires sample data in a copy/paste friendly format (like what you posted later). Please read this guide to learn how to properly provide one.

Thank you @andresrcs for your help - that worked! And apologies for not including a minimal dataset. I was working from https://reprex.tidyverse.org/ but I understand now that a minimal dataset is an additional step. Thank you again.

This topic was automatically closed 7 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.