Programming/R

[R기초] which 함수

aram 2023. 8. 1. 14:15

 

which(조건) 

  • Give the TRUE indices of a logical object, allowing for array indices.
  • 조건에 맞는 데이터의 위치 리턴
  • which.max(데이터) : 데이터 중 가장 큰 값의 위치 리턴
  • which.min(데이터) : 데이터 중 가장 작은 값의 위치 리턴
score <- c(60,40,95,80)
names(score) <- c('John','Jane','Tom','David')
score 
   # John  Jane   Tom David 
   #   60    40    95    80 
   
#85이상인 학생은?
which(score >= 85) #3 4 : 모든 위치 조회 됨
#성적이 가장 높은 학생은 어디?
max(score) #95
which.max(score) #3
#성적이 가장 낮은 학생은?
min(score) #40
which.min(score) #2
# 성적이 60점 이하인 학생의 점수를 61점으로 상향하자
score[which(score<=60)] <- 61 #61 61 95 80
# score 데이터에서 성적이 80이상인 점수를 조회
score[which(score>=80)]
subset(score, score>=80) #95 80
#성적이 가장 좋은 사람의 이름 조회
names(score)[which.max(score)]
# Jane의 성적 조회
score[which(names(socre)=='Jane')]

 

# Stat2Data 패키지

: Datasets for the textbook Stat2
: 회귀 및 분산 분석을 사용한 모델링(제2판)에 대한 데이터 세트. 첫 번째 에디션인 Stat2: Building Models for a World of Data 및 진단을 표시하기 위한 몇 가지 기능도 포함

- ChildSpeaks : 언어발달 상황 데이터셋

  • Child : 번호
  • Age : 말문 트인 시기
  • Gesell : 언어 이해력 점수
# Age 컬럼이 
# 9개월 미만이면 5등급 / 14미만이면 4등급 / 20개월 미만이면 3등급
# / 26개월 미만이면 2등급 / 그 이상은 1등급
idx <- which(ChildSpeaks$Age < 9)
ChildSpeaks[idx, 'm1'] <- 5
idx <- which(ChildSpeaks$Age >=9 & ChildSpeaks$Age < 14)
ChildSpeaks[idx, 'm1'] <- 4
idx <- which(ChildSpeaks$Age >=14 & ChildSpeaks$Age < 20)
ChildSpeaks[idx, 'm1'] <- 3
idx <- which(ChildSpeaks$Age >=20 & ChildSpeaks$Age < 26)
ChildSpeaks[idx, 'm1'] <- 2
idx <- which(ChildSpeaks$Age >=26)
ChildSpeaks[idx, 'm1'] <- 1
head(ChildSpeaks)
  #   Child Age Gesell m1
  # 1     1  15     95  3
  # 2     2  26     71  1
  # 3     3  10     83  4
  # 4     4   9     91  4
  # 5     5  15    102  3
  # 6     6  20     87  2

# Gesell 컬럼
# 70미만 1등급 / 90미만 2등급 / 110미만 3등급 / 130미만이면 4등급 / 130이상이면 5등급
idx <- which(ChildSpeaks$Gesell < 70)
ChildSpeaks[idx, 'm2'] <- 1
idx <- which(ChildSpeaks$Gesell >= 70 & ChildSpeaks$Gesell < 90)
ChildSpeaks[idx, 'm2'] <- 2
idx <- which(ChildSpeaks$Gesell >= 90 & ChildSpeaks$Gesell < 110)
ChildSpeaks[idx, 'm2'] <- 3
idx <- which(ChildSpeaks$Gesell >= 110 & ChildSpeaks$Gesell < 130)
ChildSpeaks[idx, 'm2'] <- 4
idx <- which(ChildSpeaks$Gesell >= 130)
ChildSpeaks[idx, 'm2'] <- 5
head(ChildSpeaks)
  #   Child Age Gesell m1 m2
  # 1     1  15     95  3  3
  # 2     2  26     71  1  2
  # 3     3  10     83  4  2
  # 4     4   9     91  4  3
  # 5     5  15    102  3  3
  # 6     6  20     87  2  2

# total 컬럼 생성 : m1 + m2 의 합
ChildSpeaks$total <- ChildSpeaks$m1 + ChildSpeaks$m2
ChildSpeaks$total #6 3 6 7 6 4 6 7 8 5 9 7 6 6 7 7 7 2 7 6 7

# total 값
# 3미만 매우느림 / 5미만 느림 / 7미만 보통 / 9미만 빠름 / 이상 매우 빠름
idx <- which(ChildSpeaks$total < 3)
ChildSpeaks[idx, "result"] <- "매우느림"
idx <- which(ChildSpeaks$total >=3 & ChildSpeaks$total < 5)
ChildSpeaks[idx, "result"] <- "느림"
idx <- which(ChildSpeaks$total >=5 & ChildSpeaks$total < 7)
ChildSpeaks[idx, "result"] <- "보통"
idx <- which(ChildSpeaks$total >=7 & ChildSpeaks$total < 9)
ChildSpeaks[idx, "result"] <- "빠름"
idx <- which(ChildSpeaks$total >=9)
ChildSpeaks[idx, "result"] <- "매우빠름"
ChildSpeaks
  #    Child Age Gesell m1 m2 total   result
  # 1      1  15     95  3  3     6     보통
  # 2      2  26     71  1  2     3     느림
  # 3      3  10     83  4  2     6     보통
  # 4      4   9     91  4  3     7     빠름
  # 5      5  15    102  3  3     6     보통
  # 6      6  20     87  2  2     4     느림
  # 7      7  18     93  3  3     6     보통
  # 8      8  11    100  4  3     7     빠름
  # 9      9   8    104  5  3     8     빠름
  # 10    10  20     94  2  3     5     보통
  # 11    11   7    113  5  4     9 매우빠름
  # 12    12   9     96  4  3     7     빠름
  # 13    13  10     83  4  2     6     보통
  # 14    14  11     84  4  2     6     보통
  # 15    15  11    102  4  3     7     빠름
  # 16    16  10    100  4  3     7     빠름
  # 17    17  12    105  4  3     7     빠름
  # 18    18  42     57  1  1     2 매우느림
  # 19    19  17    121  3  4     7     빠름
  # 20    20  11     86  4  2     6     보통
  # 21    21  10    100  4  3     7     빠름

# total 값이 가장 높은 아기의 정보 조회
ChildSpeaks[which.max(ChildSpeaks$total),]
  #    Child Age Gesell m1 m2 total   result
  # 11    11   7    113  5  4     9 매우빠름

 

* 내용참고&출처 : 태그의 수업을 복습 목적으로 정리한 내용입니다.