3. 데이터프레임 처리 2 (행제외, 열 제외, 행추가, 열추가, 병합, rename)
- Killxxi
- Study of GifMan/R of GifMan
- 2019. 8. 6.
데이터 프레임 처리 2¶
setwd("C:/khu")
d<- read.csv("test.csv")
d
item | price | sales |
---|---|---|
풍선 | 100 | 200 |
펌프 | 3000 | 5 |
테이프 | 300 | 20 |
플래카드 | 20000 | 1 |
색종이 | 150 | 100 |
d[,-2] # 특정 컬럼 제외
d[-2,] # 특정 행 제외
d[,c(-1,-2)] # 여러컬럼 제외
item | sales |
---|---|
풍선 | 200 |
펌프 | 5 |
테이프 | 20 |
플래카드 | 1 |
색종이 | 100 |
item | price | sales | |
---|---|---|---|
1 | 풍선 | 100 | 200 |
3 | 테이프 | 300 | 20 |
4 | 플래카드 | 20000 | 1 |
5 | 색종이 | 150 | 100 |
- 200
- 5
- 20
- 1
- 100
# 앞에서 부터 보기
head(d)
head(d, n=3)
item | price | sales |
---|---|---|
풍선 | 100 | 200 |
펌프 | 3000 | 5 |
테이프 | 300 | 20 |
플래카드 | 20000 | 1 |
색종이 | 150 | 100 |
item | price | sales |
---|---|---|
풍선 | 100 | 200 |
펌프 | 3000 | 5 |
테이프 | 300 | 20 |
# 뒤에서 부터 보기
tail(d)
tail(d, n=3)
item | price | sales |
---|---|---|
풍선 | 100 | 200 |
펌프 | 3000 | 5 |
테이프 | 300 | 20 |
플래카드 | 20000 | 1 |
색종이 | 150 | 100 |
item | price | sales | |
---|---|---|---|
3 | 테이프 | 300 | 20 |
4 | 플래카드 | 20000 | 1 |
5 | 색종이 | 150 | 100 |
# 표형태로 보기
View(d)
Error in View(d): 'View()' not yet supported in the Jupyter R kernel
Traceback:
1. View(d)
2. stop(sQuote("View()"), " not yet supported in the Jupyter R kernel")
x1 <- c(1,2,3)
x2 <- c(4,5,6)
x3 <- c(7,8,9)
d <- data.frame(x1,x2,x3)
d
x1 | x2 | x3 |
---|---|---|
1 | 4 | 7 |
2 | 5 | 8 |
3 | 6 | 9 |
x4 <- c(10,11,12)
d2 <- cbind(d, x3) # cbind는 열추가
d2
x1 | x2 | x3 | x3 |
---|---|---|---|
1 | 4 | 7 | 7 |
2 | 5 | 8 | 8 |
3 | 6 | 9 | 9 |
x5 <- c("James", "Mary", "Tony")
d3 <- cbind (d2, x5, stringAsFactors=FALSE)
d3
x1 | x2 | x3 | x3 | x5 | stringAsFactors |
---|---|---|---|---|---|
1 | 4 | 7 | 7 | James | FALSE |
2 | 5 | 8 | 8 | Mary | FALSE |
3 | 6 | 9 | 9 | Tony | FALSE |
d
x1 | x2 | x3 |
---|---|---|
1 | 4 | 7 |
2 | 5 | 8 |
3 | 6 | 9 |
x4 <- c(10,11,12)
d$x4 <- x4 # 새 컬럼을 정하면 자동으로 붙는다.
d
x1 | x2 | x3 | x4 |
---|---|---|---|
1 | 4 | 7 | 10 |
2 | 5 | 8 | 11 |
3 | 6 | 9 | 12 |
x5 <- c("James", "Mary", "Tony")
d$x5 <- x5
d
x1 | x2 | x3 | x4 | x5 |
---|---|---|---|---|
1 | 4 | 7 | 10 | James |
2 | 5 | 8 | 11 | Mary |
3 | 6 | 9 | 12 | Tony |
x1 <- c(1,2,3)
x2 <- c(4,5,6)
x3 <- c(7,8,9)
d <- data.frame(x1,x2,x3)
d
x1 | x2 | x3 |
---|---|---|
1 | 4 | 7 |
2 | 5 | 8 |
3 | 6 | 9 |
d$sum <- d$x1 +d$x2 +d$x3
d
x1 | x2 | x3 | sum |
---|---|---|---|
1 | 4 | 7 | 12 |
2 | 5 | 8 | 15 |
3 | 6 | 9 | 18 |
d$pass <- ifelse(d$sum >15, "pass", "fail") # sum이 15보다 크면 pass 그렇지 않으면 fail
d
x1 | x2 | x3 | sum | pass |
---|---|---|---|---|
1 | 4 | 7 | 12 | fail |
2 | 5 | 8 | 15 | fail |
3 | 6 | 9 | 18 | pass |
a <- data.frame(id = c(1,2,3,4,5), mid =c(30,40,50,60,70))
b <- data.frame(id = c(6,5,4,3,2,1), final = c(80,70,90,100,90,80))
a
b
id | mid |
---|---|
1 | 30 |
2 | 40 |
3 | 50 |
4 | 60 |
5 | 70 |
id | final |
---|---|
6 | 80 |
5 | 70 |
4 | 90 |
3 | 100 |
2 | 90 |
1 | 80 |
library(dplyr)
left_join(a,b,by="id") # id를 기준으로 해서 a와 b를 left join 시켜라
id | mid | final |
---|---|---|
1 | 30 | 80 |
2 | 40 | 90 |
3 | 50 | 100 |
4 | 60 | 90 |
5 | 70 | 70 |
old1 <- c(1,2,3)
old2 <- c(4,5,6)
old3 <- c(7,8,9)
d <- data.frame(old1,old2,old3)
d
old1 | old2 | old3 |
---|---|---|
1 | 4 | 7 |
2 | 5 | 8 |
3 | 6 | 9 |
d <- rename(d, new1 = old1)
d
new1 | old2 | old3 |
---|---|---|
1 | 4 | 7 |
2 | 5 | 8 |
3 | 6 | 9 |
data <- read.csv("train.csv")
head(data)
PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22 | 1 | 0 | A/5 21171 | 7.2500 | S | |
2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Thayer) | female | 38 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26 | 0 | 0 | STON/O2. 3101282 | 7.9250 | S | |
4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35 | 1 | 0 | 113803 | 53.1000 | C123 | S |
5 | 0 | 3 | Allen, Mr. William Henry | male | 35 | 0 | 0 | 373450 | 8.0500 | S | |
6 | 0 | 3 | Moran, Mr. James | male | NA | 0 | 0 | 330877 | 8.4583 | Q |
data.male <- subset(data,Sex=="male") #남성 데이터셋
data.female <- subset(data,Sex=="female") #여성 데이터셋
# 10미만,10대,20대 데이터 분리
data.male0 <- subset(data.male, Age<10&Age>=0)
data.male10 <- subset(data.male, Age<20&Age>=10)
data.male20<- subset(data.male, Age<30&Age>=20)
data.female0 <- subset(data.female, Age<10&Age>=0)
data.female10 <- subset(data.female, Age<20&Age>=10)
data.female20 <- subset(data.female, Age<30&Age>=20)
# 남성 생존률
mean(data.male0$Survived) #남성이면서 10대 미만의 생존률
mean(data.male10$Survived) #남성이면서 10대의 생존률
mean(data.male20$Survived) #남성이면서 20대의 생존률
# 여성 생존률
mean(data.female0$Survived) #여성이면서 10대 미만의 생존률
mean(data.female10$Survived) #여성이면서 10대의 생존률
mean(data.female20$Survived) #여성이면서 20대의 생존률/
0.59375
0.12280701754386
0.168918918918919
0.633333333333333
0.755555555555556
0.722222222222222
'Study of GifMan > R of GifMan' 카테고리의 다른 글
1. Tableau 이해하기 (0) | 2019.08.07 |
---|---|
4. 데이터 분리(sample, split, ifelse) (0) | 2019.08.06 |
2. 데이터 프레임 처리 1 (data.frame, stringAsFactors, dplyr패키지, which, subset) (0) | 2019.08.06 |
1. Feature Engineering(Scaling, Binnig, Creating Feature, as.Date, Creating Dummy) (0) | 2019.08.06 |
5. 집단 차이 분석(일표본,이표본 평균(t.test), 일표본 비율(binom.test), 이표본 비율( prop.test), (0) | 2019.08.06 |