getwd() setwd(“/Users/cross/MKTG intro to R”)
library(ggplot2) library(readxl) library(tidyverse) library(dplyr) str(Car_Survey_1) #display the first few rows head(Car_Survey_1,n=10)
#read excel car survey 2 str(Car_Survey_2) head(Car_Survey_2,n=5)
##renaming the first column name in car1 names(Car_Survey_2)[1]<-c(“Resp”) head(Car_Survey_2, n=1)
#merge them Car_Total<-merge(Car_Survey_1,Car_Survey_2, by=“Resp”) str(Car_Total) head(Car_Total)
#use car total for all anaylsis mean(Car_Total$Att_1)
na_rows <- Car_Total[is.na(Car_Total$Att_1),] print(na_rows)
meanATT1<-mean(Car_Total$Att_1,na.rm=TRUE) print(meanATT1)
Car_Total[is.na(Car_Total$Att_1), “Att_1”] <- meanATT1
Car_Total[c(rownames(na_rows)),]
Car_Total\(Att_Mean = (Car_Total\)Att_1 + Car_Total$Att_2) / 2 View(Car_Total[c(“Att_1”, “Att_2”, “Att_Mean”)])
na_rows <- Car_Total[is.na(Car_Total$Att_Mean),] print(na_rows)
#graph library(ggplot2) ggplot(Car_Total,aes(x=Region, fill = Region))+ theme_bw()+ geom_bar()+ geom_text(stat=“count”, aes(label=after_stat(count)), vjust=0) + labs(y=“Number of Cars”, x = “Region”, title = “Number of Cars by Region”)
Car_Total\(Model<-as.factor(Car_Total\)Model) Car_Total\(Region<-as.factor(Car_Total\)Region)
ggplot(Car_Total,aes(x=Region,fill = Model))+ theme_bw()+ geom_bar()+ labs(y=“Number of Cars”, title = “Number of Cars by Model and Region”)
#make by region library(dplyr) library(stringr)
Car_Total <- Car_Total %>% mutate( Make = word(Model, 1), # Extract the first word as Make Model_v1 = str_trim(str_remove(Model, paste0(“^”, word(Model, 1), ” “))) # Remove the first word and trim any whitespace )
View(Car_Total)
table(Car_Total$Make)
freq_table <- Car_Total %>% count(Make, Model_v1, name = “Freq”) %>% arrange(desc(Freq))
print(freq_table)
ggplot(Car_Total,aes(x=Region,fill=Make))+ theme_bw()+ geom_bar()+ labs(y=“Number of Cars”, title = “Number of Cars per make by Region”)
Car_Total <- Car_Total %>% mutate(Parent = case_when( Make == “Buick” ~ “General Motors”, Make == “Chevrolet” ~ “General Motors”, Make == “Chrysler” ~ “Chrysler”, Make == “Dodge” ~ “Chrysler”, Make == “Fiat” ~ “Chrysler”, Make == “Ford” ~ “Ford”, Make == “Honda” ~ “Honda”, Make == “Kia” ~ “Kia”, Make == “Lincoln” ~ “Ford”, Make == “Toyota” ~ “Toyota”, TRUE ~ “Check” # Default case for unrecognized Makes ))
count(Car_Total, Car_Total\(Make, Car_Total\)Parent, name = “Freq”) table(Car_Total$Make)
ggplot(Car_Total,aes(x=Region,fill=Parent))+ theme_bw()+ geom_bar()+ labs(y=“Number of Cars”, title = “Number of Cars per Parent Company by Region”)
brand_region_table <- aggregate(Att_1~Parent+Region, Car_Total, mean) print(brand_region_table)
#age 2
ford_cars <- Car_Total %>% filter(Make == “Ford”)
ford_cars\(AgeGrp <- cut(ford_cars\)Age, breaks = c(0, 30, 50, Inf), labels = c(“Young Adults”, “Adults”, “Mature adults”), right = FALSE)
#Graph ggplot(ford_age_group_counts, aes(x = AgeGrp, y = n, fill = AgeGrp)) + geom_bar(stat = “identity”) + labs( title = “Distribution of Age Groups for Ford Cars”, x = “Age Group”, y = “Number of Ford Cars” ) + theme_minimal() + scale_fill_brewer(palette = “Set3”) # Optional: Change color palette
Car_Total\(AgeGrp <- cut(Car_Total\)Age, breaks = c(0, 30, 50, Inf), labels = c(“Young Adults”, “Adults”, “Mature adults”), right = FALSE)
make_age_counts <- Car_Total %>% group_by(AgeGrp, Make) %>% summarize(Count = n(), .groups = ‘drop’)
print(make_age_counts)
ggplot(make_age_counts, aes(x = AgeGrp, y = Count, fill = Make)) + geom_bar(stat = “identity”, position = “dodge”) + # Use position = “dodge” for side-by-side bars labs( title = “Distribution of Car Make by Age Group”, x = “Age Group”, y = “Number of Cars” ) + theme_minimal() + scale_fill_brewer(palette = “Set3”)
Car_Total\(Valu_Percp_Mean = (Car_Total\)Valu_Percp_1 + Car_Total$Valu_Percp_2) /2 View(Car_Total[c(“Valu_Percp_1”, “Valu_Percp_2”, “Valu_Percp_Mean”)])
avg_valu_percp_by_model <- Car_Total %>% group_by(Model) %>% summarise(avg_valu_percp = mean(Valu_Percp_Mean, na.rm = TRUE))
print(avg_valu_percp_by_model)
#Graph
ggplot(avg_valu_percp_by_model, aes(x = Model, y = avg_valu_percp, fill = Model)) + geom_bar(stat = “identity”) + theme_minimal() + labs( title = “Mean Value Perceived Score by Car Model”, x = “Car Model”, y = “Mean Value Perceived Score” ) + theme(axis.text.x = element_text(angle = 45, hjust = 1))