Remove Single Spaces From String

84 views Asked by At

I am working to parse some difficult data and need to remove just the single spaces. The rows of my data look like the example below

[1] "  Class                                Dist   Quantity    Market   Taxable/$                        "
[2] " 4-2101 THIS LAND                     28       108.85    216797     6352.00                        "
[3] "99-9084 FIRE PROTECTION               9084       0.00         0       26.95                        "
[4] "99-9093 COUNTY VALLEY SOIL            9093       0.00         0     6352.00                        "

If I could condense the hyphenated numbers and descriptions by removing the single-spaces, I could then read the text using read.table.

How could I iterate through the rows and replace just the single spaces with no-spaces? The resulting data would look like

[1] "  Class                                Dist   Quantity    Market   Taxable/$                        "
[2] " 4-2101THISLAND                     28       108.85    216797     6352.00                        "
[3] "99-9084FIREPROTECTION               9084       0.00         0       26.95                        "
[4] "99-9093COUNTYVALLEYSOIL            9093       0.00         0     6352.00                        "
2

There are 2 answers

0
ytk On BEST ANSWER

You can use the gsub command.

data$Class <- gsub("\\s{1}(\\S)", "\\1", data$Class)

Edited as suggested by rawr.

0
akrun On

You could also do this without removing the spaces between the words that belong to the 'Class' column.

v1 <- gsub('^\\s+|\\s+$', '', v1)
v1[-1] <- gsub('(?<=[A-Za-z]|^\\b)\\s*(?=\\d+)', "'", v1[-1], perl=TRUE)
 read.table(text=v1, header=TRUE, stringsAsFactors=FALSE, check.names=FALSE)
 #                       Class Dist Quantity Market Taxable/$
 #1           4-2101 THIS LAND   28   108.85 216797   6352.00
 #2    99-9084 FIRE PROTECTION 9084     0.00      0     26.95
 #3 99-9093 COUNTY VALLEY SOIL 9093     0.00      0   6352.00

data

v1 <- c("  Class                                Dist   Quantity    Market   Taxable/$                        ", 

 " 4-2101 THIS LAND                     28       108.85    216797     6352.00                        ", 
 "99-9084 FIRE PROTECTION               9084       0.00         0       26.95                        ", 
 "99-9093 COUNTY VALLEY SOIL            9093       0.00         0     6352.00                        "
)