replace_with_na 범위 지정(Scoped) 변형 사용하기
NA로 값을 바꿀 때 코드 반복을 줄이려면 replace_with_na()의 "범위 지정(Scoped) 변형"을 사용하세요:
replace_with_na_at()replace_with_na_if()replace_with_na_all()
치환 구문의 형태는 다음과 같습니다:
~.x == "N/A"
이는 "N/A"와 동일한 모든 경우를 바꿉니다.
~.x %in% c("N/A", "missing", "na", " ")
"N/A", "missing", "na", 또는 " "를 가진 모든 경우를 바꿉니다.
이 연습은 강의의 일부입니다
R에서 결측치 다루기
연습 안내
데이터셋 pacman에 대해 동일한 특수 누락값 "N/A", "missing", "na", " "을(를) 바꾸세요:
replace_with_na_at()을(를) 사용해year,month,day에서.replace_with_na_if()을(를) 사용해 문자형 변수만.replace_with_na_all()을(를) 사용해 모든 변수에서.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Use `replace_with_na_at()` to replace with NA
replace_with_na_at(pacman,
.vars = c(___, ___, ___),
~.x %in% c(___, ___, ___, ___))
# Use `replace_with_na_if()` to replace with NA the character values using `is.character`
replace_with_na_if(pacman,
.predicate = ___,
~.x %in% c(___, ___, ___, ___))
# Use `replace_with_na_all()` to replace with NA
replace_with_na_all(___, ___ %in% c(___, ___, ___, ___))