data_example <- data.frame(
region = c("North", "South", "East", "West", "North", "South", "East", "West"),
product = c("A", "A", "A", "A", "B", "B", NA, "B"),
sales = c(100, 150, NA, 250, 120, 170, 220, 270),
year = c(2020, 2020, 2020, NA, 2021, 2021, 2021, 2021),
gender = c("male", "female", "male", "female", "male", "female", "male", "female"),
satisfaction = c("very satisfied", "satisfied", "neutral", "unsatisfied", "very unsatisfied", "satisfied", "neutral", "very satisfied")
)
head(data_example)
## region product sales year gender satisfaction
## 1 North A 100 2020 male very satisfied
## 2 South A 150 2020 female satisfied
## 3 East A NA 2020 male neutral
## 4 West A 250 NA female unsatisfied
## 5 North B 120 2021 male very unsatisfied
## 6 South B 170 2021 female satisfied
colSums(is.na(data_example))
## region product sales year gender satisfaction
## 0 1 1 1 0 0
mean_scores <- mean(data_example$sales, na.rm = TRUE)
data_example$sales[is.na(data_example$sales)] <- mean_scores
Menghampus colom
clean_df<-na.omit(data_example)
head(clean_df)
## region product sales year gender satisfaction
## 1 North A 100.0000 2020 male very satisfied
## 2 South A 150.0000 2020 female satisfied
## 3 East A 182.8571 2020 male neutral
## 5 North B 120.0000 2021 male very unsatisfied
## 6 South B 170.0000 2021 female satisfied
## 8 West B 270.0000 2021 female very satisfied
head(data_example)
## region product sales year gender satisfaction
## 1 North A 100.0000 2020 male very satisfied
## 2 South A 150.0000 2020 female satisfied
## 3 East A 182.8571 2020 male neutral
## 4 West A 250.0000 NA female unsatisfied
## 5 North B 120.0000 2021 male very unsatisfied
## 6 South B 170.0000 2021 female satisfied
data_example$gende_num <- as.numeric(factor(data_example$gender, levels= c("male","female")))
data_example$satisfaction_num <-as.numeric(factor(data_example$satisfaction,levels=c("unsatisfied","neutral","satisfied","very satisfied")))
head(data_example)
## region product sales year gender satisfaction gende_num
## 1 North A 100.0000 2020 male very satisfied 1
## 2 South A 150.0000 2020 female satisfied 2
## 3 East A 182.8571 2020 male neutral 1
## 4 West A 250.0000 NA female unsatisfied 2
## 5 North B 120.0000 2021 male very unsatisfied 1
## 6 South B 170.0000 2021 female satisfied 2
## satisfaction_num
## 1 4
## 2 3
## 3 2
## 4 1
## 5 NA
## 6 3
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
g<-ggplot(data_example,aes(x = region,y = sales,fill=region))+geom_col(size = 3,alpha =0.5)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
g<-ggplotly(g)
g
g <- ggplot(data_example, aes(x = region)) +
geom_density(size= 1,alpha = 0.5,fill="red" ) + theme_minimal()+ facet_wrap(~region)
# Menampilkan plot
print(g)
x<-ggplot(data_example,aes(y = sales))+ geom_boxplot(alpha = 0.5)
x <- ggplotly(x)
x
g <- ggplot(data_example, aes(x = region, y = sales, fill = region)) +
geom_boxplot(alpha = 0.5)
g