월별 열 값이 있습니다.
df <- data.frame(Month= c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3"))
1 월, 2 월, 3 월, 4 월, 5 월, 6 월, 7 월, 8 월, 9 월, 10 월, 11 월, 12 월, 1 월, 2 월, 3 월로 변환하고 싶습니다.
누구든지 이것으로 나를 도울 수 있습니까?
시간을 내 주셔서 미리 감사드립니다.
가장 우아하거나 프로그래밍 방식의 솔루션은 아니지만 모든 값을 패키지 의 mutate()
함수로 바꿀 수 있습니다 dplyr
.
library(dplyr)
df = mutate(df, Month = case_when(Month == "1" ~ "Jan",
Month == "2" ~ "Feb",
Month == "3" ~ "Mar",
Month == "4" ~ "Apr",
Month == "5" ~ "May",
Month == "6" ~ "Jun",
Month == "7" ~ "Jul",
Month == "8" ~ "Aug",
Month == "9" ~ "Sep",
Month == "10" ~ "Oct",
Month == "11" ~ "Nov",
Month == "12" ~ "Dec")
case_when()
여기 에서 사용 설명서를 찾을 수 있습니다 : https://dplyr.tidyverse.org/reference/case_when.html
내장 R 객체를 사용하여 한 줄에이 작업을 수행하기 위해 @ r2evans가 남긴 주석을 기반으로하는 대체 프로그래밍 솔루션 month.abb
:
df = mutate(df, Month = month.abb[as.numeric(Month)])
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다