아래 데이터프레임에서 성별(Gender)로 그룹을 묶으세요.
library(tibble)
library(dplyr)
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
df <- tibble(
Customer_ID = 1:6,
Gender = c("Male", "Female", "Female", "Male", "Male", "Female"),
Total_Spend = c(100000, 150000, 200000, 120000, 170000, 130000)
)
위에서 만든 그룹에서 성별별 총 구매금액 합계를 구하세요.
sum() 함수를 사용합니다.고객별로 구매금액이 150,000 이상이면 “High”, 미만이면 “Low”라는 새로운 변수를 추가하세요.
ifelse()를 사용합니다.지역(Region)별로 가장 많은 구매횟수(Purchase_Frequency)의 평균을 구하세요.
df <- tibble(
Customer_ID = 1:8,
Region = c("Seoul", "Busan", "Seoul", "Gwangju", "Busan", "Daejeon", "Seoul", "Busan"),
Purchase_Frequency = c(3, 5, 2, 4, 6, 2, 7, 3)
)
나이에 따라 다음과 같이 새로운 열을 추가하세요. (Age_Group)
df <- tibble(
Customer_ID = 1:6,
Age = c(22, 35, 47, 52, 28, 61)
)
case_when()을 사용합니다.주어진 데이터에서: - 데이터를 병합합 - 지역별로 총 구매금액 합계를 구하고 - 합계가 400,000 이상이면 “Top Region”, 아니면 “Normal Region”으로 구분하는 열을 추가하세요.
library(tibble)
library(dplyr)
df1 <- tibble(
Customer_ID = 1:6,
Gender = c("Male", "Female", "Female", "Male", "Male", "Female"),
Total_Spend = c(100000, 150000, 200000, 120000, 170000, 130000)
)
df2 <- tibble(
Customer_ID = 1:6,
Region = c("Seoul", "Busan", "Seoul", "Gwangju", "Busan", "Daejeon"),
Purchase_Frequency = c(3, 5, 2, 4, 6, 2)
)