beddotcom
beddotcom

Reputation: 507

Inserting missing quotes around strings

I'm working with messy data consisting of thousands of strings (example below).

N        Text_Strings
1        c("israel v. bulgaria", "israel v. bulgaria")
2        israel v. bulgaria
3        c("israel v. bulgaria", "israel v. bglgaria")
4        israel v. bulgaria
5        character(0)
...

As is apparent from the sample above/MWE below, strings with only one element are missing quotes, while strings with multiple, concatenated substrings contain escaped quotes. My actual data is 11,000 rows long and contains 150+ unique substrings.

How can I either (a) remove quotes in strings with multiple substrings or (b) insert them where missing? There are a ton of resources on SO explaining how to paste in quotes when target substrings are known, but I couldn't find anything on how to do it for all rows conditionally.

Thanks in advance for the help!

x <- structure(list(case_num = structure(c(34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L),
                                     .Label = c("  1", "  3", "  4", "  5", 
                                                "  6", "  7", "  8", "  9", " 10", " 11", " 12", " 13", " 14", 
                                                " 15", " 16", " 17", " 18", " 19", " 20", " 21", " 22", " 23", 
                                                " 24", " 25", " 26", " 27", " 28", " 29", " 30", " 31", " 32", 
                                                " 33", " 34", " 35", " 36", " 37", " 38", " 39", " 40", " 41", 
                                                " 42", " 43", " 44", " 45", " 46", " 47", " 48", " 49", " 50", 
                                                " 51", " 52", " 53", " 54", " 55", " 56", " 57", " 58", " 59", 
                                                " 60", " 61", " 62", " 63", " 64", " 65", " 66", " 67", " 68", 
                                                " 69", " 70", " 71", " 72", " 73", " 74", " 75", " 76", " 77", 
                                                " 78", " 79", " 80", " 81", " 82", " 83", " 84", " 85", " 86", 
                                                " 87", " 88", " 89", " 90", " 91", " 92", " 93", " 94", " 95", 
                                                " 96", " 97", " 98", " 99", "100", "101", "102", "103", "104", 
                                                "105", "106", "107", "108", "109", "110", "111", "112", "113", 
                                                "114", "115", "116", "117", "118", "119", "120", "121", "122", 
                                                "123", "124", "125", "126", "127", "128", "129", "130", "131", 
                                                "132", "133", "134", "135", "136", "137", "138", "139", "140", 
                                                "141", "142", "143", "144", "145", "146", "147", "148", "149", 
                                                "150", "151", "152", "153", "154", "155", "156", "157", "158", 
                                                "159", "160", "161", "162", "163", "164"),
                                     class = "factor"), 
                type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L),
                                 .Label = c("court", "claimant"),
                                 class = "factor"),
                listcites = c("c(\"israel v. bulgaria\", \"israel v. bulgaria\")",
                              "israel v. bulgaria",
                              "c(\"israel v. bulgaria\", \"israel v. bglgaria\")",
                              "israel v. bulgaria",
                              "character(0)",
                              "character(0)",
                              "character(0)",
                              "character(0)",
                              "character(0)",
                              "character(0)")),
           .Names = c("case_num", "type", "listcites"),
           row.names = c(485L, 486L, 487L, 488L, 489L, 490L, 491L, 492L, 495L, 496L),
           class = "data.frame")

Upvotes: 0

Views: 810

Answers (1)

manotheshark
manotheshark

Reputation: 4357

This can be accomplished with regex. To remove quotes in strings

x$listcities <- gsub("\"", "", x$listcites)

To also remove the leading c( and trailing )

x$listcities <- gsub("^c\\(|\"|\\)$", "", x$listcites)

Upvotes: 1

Related Questions