0

After reading an HTML table, my name column appears with records as follows:

\n\t\t\t\t\t\t\t\t\t\t\t\t\tMike Moon\n\t\t\t\t\t\t\t\t

The following code fails to generate the correct values in the First and Last name columns

separate(data=nametable, col = Name, into = c("First","Last"), sep= " ")

Curiously, the First column is blank, while the Last column contains only the person's first name.

How could I correctly turn this column into the First and Last column desired (i.e...

First     Last
Mike      Moon

Data example per recommendation of @r2evans and as appearing in correct answer code below:

nametable <- data.frame(Name="\n\t\t\t\t\t\t\t\t\t\t\t\t\tMike Moon\n\t\t\t\t\t\t\t\t", stringsAsFactors=FALSE)
Pake
  • 968
  • 9
  • 24
  • Please make this question [reproducible](https://stackoverflow.com/questions/5963269/). This starts with providing reusable data, such as `dput(head(nametable))`. The next step is ensuring you include all non-base packages, where I am assuming you mean `tidyr::separate`. – r2evans Aug 21 '18 at 14:58
  • If I make a tibble with just this string in it, `tidyr::separate` works for getting first & last names; they just each have lots of escaped characters. So there might be something missing from your post to recreate the problem – camille Aug 21 '18 at 15:01
  • 1
    @r2evans thank you for the suggestion and for the quick answer. I've added the example data as reflected in your answer code to the original question. – Pake Aug 21 '18 at 15:10

1 Answers1

1

It might help to trim whitespace from the field before moving on. trimws removes "leading and/or trailing whitespace from character strings" (from ?trimws).

Data:

nametable <- data.frame(Name="\n\t\t\t\t\t\t\t\t\t\t\t\t\tMike Moon\n\t\t\t\t\t\t\t\t", stringsAsFactors=FALSE)

library(dplyr)
nametable %>% mutate(Name = trimws(Name))
#        Name
# 1 Mike Moon

I infer that you are using dplyr as well as tidyr, so I'm using it here. It is also really straight-forward to do nametable$Name <- trimws(nametable$Name) without the dplyr usage. From here, it's as you initially coded:

nametable %>%
  mutate(Name = trimws(Name)) %>%
  tidyr::separate(col=Name, into=c("First", "Last"))
#   First Last
# 1  Mike Moon
r2evans
  • 141,215
  • 6
  • 77
  • 149