The R data set mtcars was extracted from the 1974 Motor Trend US magazine, and c
ID: 3306737 • Letter: T
Question
The R data set mtcars was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models). To load the data to R, use
> data(mtcars)
1-Get the row names of the data frame mtcars.
2-Split each row name to two or three strings using “ ” (white space) as the delimiter.
3-Select the first string from each split results and select only the first three letters of the first string.
4-Combine each split result back to the original row name.
5-Count the number of row names that contains “Toyota” using the original row names.
Explanation / Answer
The complete R snippet is as follows
data(mtcars)
mtcars
## rownames
row_names <- rownames(mtcars)
row_names
# read the names into a dataframe
df <- data.frame(row_names)
strsplit(row_names, " ")
## extract the first and the last names
first_string <- sapply(strsplit(row_names, " "), "[[", 1)
first_string
first_string_3 <- substr(first_string, 1, 3)
first_string_3
l = c( "Toyota")
subset(df, grepl(l[1], df$row_names))
#take the count
nrow(subset(df, grepl(l[1], df$row_names)))
The results are
> row_names
[1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive"
[5] "Hornet Sportabout" "Valiant" "Duster 360" "Merc 240D"
[9] "Merc 230" "Merc 280" "Merc 280C" "Merc 450SE"
[13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood" "Lincoln Continental"
[17] "Chrysler Imperial" "Fiat 128" "Honda Civic" "Toyota Corolla"
[21] "Toyota Corona" "Dodge Challenger" "AMC Javelin" "Camaro Z28"
[25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2" "Lotus Europa"
[29] "Ford Pantera L" "Ferrari Dino" "Maserati Bora" "Volvo 142E"
> strsplit(row_names, " ")
[[1]]
[1] "Mazda" "RX4"
[[2]]
[1] "Mazda" "RX4" "Wag"
[[3]]
[1] "Datsun" "710"
[[4]]
[1] "Hornet" "4" "Drive"
[[5]]
[1] "Hornet" "Sportabout"
[[6]]
[1] "Valiant"
[[7]]
[1] "Duster" "360"
[[8]]
[1] "Merc" "240D"
[[9]]
[1] "Merc" "230"
[[10]]
[1] "Merc" "280"
[[11]]
[1] "Merc" "280C"
[[12]]
[1] "Merc" "450SE"
[[13]]
[1] "Merc" "450SL"
[[14]]
[1] "Merc" "450SLC"
[[15]]
[1] "Cadillac" "Fleetwood"
[[16]]
[1] "Lincoln" "Continental"
[[17]]
[1] "Chrysler" "Imperial"
[[18]]
[1] "Fiat" "128"
[[19]]
[1] "Honda" "Civic"
[[20]]
[1] "Toyota" "Corolla"
[[21]]
[1] "Toyota" "Corona"
[[22]]
[1] "Dodge" "Challenger"
[[23]]
[1] "AMC" "Javelin"
[[24]]
[1] "Camaro" "Z28"
[[25]]
[1] "Pontiac" "Firebird"
[[26]]
[1] "Fiat" "X1-9"
[[27]]
[1] "Porsche" "914-2"
[[28]]
[1] "Lotus" "Europa"
[[29]]
[1] "Ford" "Pantera" "L"
[[30]]
[1] "Ferrari" "Dino"
[[31]]
[1] "Maserati" "Bora"
[[32]]
[1] "Volvo" "142E"
> first_string
[1] "Mazda" "Mazda" "Datsun" "Hornet" "Hornet" "Valiant" "Duster" "Merc"
[9] "Merc" "Merc" "Merc" "Merc" "Merc" "Merc" "Cadillac" "Lincoln"
[17] "Chrysler" "Fiat" "Honda" "Toyota" "Toyota" "Dodge" "AMC" "Camaro"
[25] "Pontiac" "Fiat" "Porsche" "Lotus" "Ford" "Ferrari" "Maserati" "Volvo"
> first_string_3
[1] "Maz" "Maz" "Dat" "Hor" "Hor" "Val" "Dus" "Mer" "Mer" "Mer" "Mer" "Mer" "Mer" "Mer" "Cad"
[16] "Lin" "Chr" "Fia" "Hon" "Toy" "Toy" "Dod" "AMC" "Cam" "Pon" "Fia" "Por" "Lot" "For" "Fer"
[31] "Mas" "Vol"
> subset(df, grepl(l[1], df$row_names))
row_names
20 Toyota Corolla
21 Toyota Corona
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.