0

Need to see maker names against car models

something like this: enter image description here

trying to use below function, but it is creating as list

strsplit(carz$maker,split = " ")
Til
  • 5,150
  • 13
  • 26
  • 34
  • Possible duplicate of [Split data frame string column into multiple columns](https://stackoverflow.com/questions/4350440/split-data-frame-string-column-into-multiple-columns) – camille May 25 '19 at 21:00

3 Answers3

1

Here is an approach that uses lapply() with the Motor Trend Cars data frame.

data(mtcars)
mtcars$type <- rownames(mtcars)
mtcars$make <-unlist(lapply(strsplit(mtcars$type," "),function(x){x[[1]]}))
head(mtcars)

and the result:

> head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
                               type    make
Mazda RX4                 Mazda RX4   Mazda
Mazda RX4 Wag         Mazda RX4 Wag   Mazda
Datsun 710               Datsun 710  Datsun
Hornet 4 Drive       Hornet 4 Drive  Hornet
Hornet Sportabout Hornet Sportabout  Hornet
Valiant                     Valiant Valiant
> 

Note, that some additional data cleaning is necessary, because the Valiant and Duster were made by Plymouth, the Camaro Z28 was made by Chevrolet, and the Hornet 4 Drive was made by American Motor Cars, also known as AMC.

Regarding the question in the comments about the syntax used within lapply(), I used lapply() to process the results of strsplit(), including an anonymous function that extracts the first word from each element of the list.

Since the output of an R function can be used as the argument to another function, this solution nests functions to produce the desired result.

The sapply() answer provided by akrun does the same thing, using output from strsplit() as its input, and using [, one of the four forms of the extract operator to extract the data. sapply() also produces a vector rather than a list as its output.

Len Greski
  • 10,505
  • 2
  • 22
  • 33
  • Thanks it really worked for me, however if you can explain about function(x){x[[1]]}) it would be helpful for me. – varun Bollam May 25 '19 at 15:43
  • @varunBollam - sure, it does exactly the same thing as @akrun use of `sapply()`, as we posted our answers at about the same time. R supports a programming construct called anonymous functions, one of the basic building blocks of functional programming. They can also use within the `apply()` familiy of functions. I'll update my answer to provide additional explanation. – Len Greski May 25 '19 at 15:53
1

And of course, there's a tidyverse solution too; this is what the separate function was designed for.

library(tidyverse)
mtcars %>% rownames_to_column("type") %>% 
  separate(type, c("make", "model"), 
           extra="merge", fill="right", remove=FALSE) 

Showing a selection of the output:

                  type     make       model
1            Mazda RX4    Mazda         RX4
2        Mazda RX4 Wag    Mazda     RX4 Wag
3           Datsun 710   Datsun         710
4       Hornet 4 Drive   Hornet     4 Drive
5    Hornet Sportabout   Hornet  Sportabout
6              Valiant  Valiant        <NA>
7           Duster 360   Duster         360
8            Merc 240D     Merc        240D
9             Merc 230     Merc         230
10            Merc 280     Merc         280
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
0

The strsplit retuns a list, we need to loop through the list and extract the first word to get a vector

carz$maker <- sapply(strsplit(carz$maker,split = " "), `[`, 1)

Reproducible with mtcars

sapply(strsplit(rownames(mtcars), " "), `[`, 1)
#[1] "Mazda"    "Mazda"    "Datsun"   "Hornet"   "Hornet"   "Valiant"  "Duster"   "Merc"     "Merc"     "Merc"     "Merc"    
#[12] "Merc"     "Merc"     "Merc"     "Cadillac" "Lincoln"  "Chrysler" "Fiat"     "Honda"    "Toyota"   "Toyota"   "Dodge"   
#[23] "AMC"      "Camaro"   "Pontiac"  "Fiat"     "Porsche"  "Lotus"    "Ford"     "Ferrari"  "Maserati" "Volvo"   

It can also be done without strsplit

carz$maker <- sub("\\s+.*", row.names(carz))

Or with word

library(stringr)
word(carz$maker, 1)
akrun
  • 874,273
  • 37
  • 540
  • 662