Home > Software engineering >  How to extract specific column?
How to extract specific column?

Time:04-03

I am dealing with mpg data in R. I want to learn which type of manufacturer that did not produce any manual cars? I need to draw a bar plot by using str_remove() function to edit the trans column so I can have clearer plot.

Can you help me how to do this?

CodePudding user response:

If you look at the trans column, every entry contains either the word "auto" or "manual", followed by some parentheses containing the transmission's sub-type. We can simplify this column into either just "manual" or "auto" by removing the parentheses using str_remove. Then we can remove the "auto" models, and count the number of models of manual transmissions per manufacturer. We can then plot the number of manual models per manufacturer.

library(tidyverse)

mpg %>%
  mutate(trans = str_remove(trans, "\\(.*\\)")) %>%
  group_by(manufacturer) %>%
  summarize(count = sum(trans == "manual")) %>%
  ggplot(aes(manufacturer, count))  
  geom_col()  
  ggtitle("Number of manual transmission models produced per manufacturer")

enter image description here

We can see that Jeep, Landrover, Lincoln, Mercury and Pontiac have no manual transmission models in this data set.

CodePudding user response:

Is your question how to select certain columns or filter?

If it is how to select, then simply using the tidyverse package and you use the select() argument like below

df<- mpg %>% select(column_name)

if you want to filter a column then use filter() to pass an argument

df<- mpg %>% filter(column_name==argument_1)
  • Related