-1

The dataset that is used in many R tutorials has an initial column with no header that lists the names of the cars, according to make and model.

I was trying to separate the make and the model of car.

First I made a column of the first, headerless column, called "names".

> mtcars$names <- rownames(mtcars)

> rownames(mtcars) <- NULL

This gave me a column that was a character vector with the following results:

> mtcars$names

With the following output:

[1] "Mazda RX4"           "Mazda RX4 Wag"    
[3] "Datsun 710"          "Hornet 4 Drive"   

etc

I then tried this:

separate(mtcars, colname = names, into = c('make', 'model')

and got the following error:

Error: Please supply column name

  • 2
    The argument is `col`, not `colname`. – aosmith Aug 02 '16 at 19:09
  • If the issue is splitting on the first space, you can use `extra = "merge"` in `separate` rather than working out the regex. See [here](http://stackoverflow.com/questions/37126634/r-tidyr-separate-only-first-n-instances/37126832) – aosmith Aug 02 '16 at 20:40

1 Answers1

0
separate(mtcars, col = names, into = c('make', 'model'), sep = '')

The "Vaillant" does not have a make description, but I think this is what you were asking for.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
Tim-TU
  • 408
  • 1
  • 4
  • 13
  • This will do it, but to make it complete please address the real issue of OP attempting to pass an incorrect argument name (`colname` where it should be `col`), which is what triggered the error. – Rich Scriven Aug 02 '16 at 19:20
  • [http://stackoverflow.com/questions/8299978/splitting-a-string-on-the-first-space] Here you can find a much better solution – Tim-TU Aug 02 '16 at 19:25
  • How could you tell that the Valiant did not have a make description. To get separate to work (along with extra = 'merge' ) I had to delete that row. – Ernie Cranks Aug 03 '16 at 16:01
  • @ErnieCranks Is your tidyr version up to date? `separate` works fine for me on this example, just puts `NA` in the second column for Valiant and gives a warning. – aosmith Aug 04 '16 at 14:37