0

After reading my .CSV file in R, I have a lot of extra spaces in between. How do I eliminate these?

file = read.csv("filename.csv")

After reading the file, I get lot of spaces in between, how to remove in R?

a = data.frame(Answer = c("who doesn't like some appreciation now and then                                                                     all employees can give receive non monetary awards in the form of e cards please use the recognition awards and follow instructions there to send an e card want to send some appreciation my way          ")

In another situation, I have used below code to remove apostrophe such as doesn't so that I get doesnt but I am getting doesn t:

     a$Answer=gsub('[[:punct:] ]+',' ',a$Answer)

Which is the correct way to remove apostrophe without getting a space?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
alice
  • 23
  • 4
  • Can you please tell me what this does? – alice Apr 28 '19 at 12:14
  • I think this would work. `gsub("\\s+", " ", trimws(a$Answer))`. removing more than one space with only one space. – Ronak Shah Apr 28 '19 at 12:21
  • 1
    Possible duplicate of [Merge Multiple spaces to single space; remove trailing/leading spaces](https://stackoverflow.com/questions/25707647/merge-multiple-spaces-to-single-space-remove-trailing-leading-spaces) – A. Suliman Apr 28 '19 at 12:22
  • As for the punctuation issue, `gsub` takes the form `gsub(pattern, replacement, string)`. As you can see, in your `a$Answer=gsub('[[:punct:] ]+', ' ', a$Answer)` your replacement is a space, `' '`. If you want your replacement to be nothing--not even a space, use `a$Answer=gsub('[[:punct:] ]+', '', a$Answer)`. (I'm also surprised it's working with the space between the close brackets `] ]`. I would change to `a$Answer=gsub('[[:punct:]]+', '', a$Answer)`. – Gregor Thomas Apr 28 '19 at 12:25

1 Answers1

0
  1. Space Issue

For the space issue i think the str_squish does the job. It comes from the package stringr. (if you do no have it, make sure you installed it before you load the library)

install.packages(stringr)
library(stringr)
a$Answer= str_squish(a$Answer)