반응형
경고 메시지:에서...: 잘못된 요인 수준, NA가 생성됨
제가 왜 이런 경고 메시지를 받았는지 이해할 수 없습니다.
> fixed <- data.frame("Type" = character(3), "Amount" = numeric(3))
> fixed[1, ] <- c("lunch", 100)
Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = "lunch") :
invalid factor level, NA generated
> fixed
Type Amount
1 <NA> 100
2 0
3 0
경고 메시지는 "유형" 변수가 요인이 되고 "점심"이 정의된 수준이 아니기 때문입니다.사용stringsAsFactors = FALSE
데이터 프레임을 만들 때 플래그를 눌러 "유형"을 문자로 만듭니다.
> fixed <- data.frame("Type" = character(3), "Amount" = numeric(3))
> str(fixed)
'data.frame': 3 obs. of 2 variables:
$ Type : Factor w/ 1 level "": NA 1 1
$ Amount: chr "100" "0" "0"
>
> fixed <- data.frame("Type" = character(3), "Amount" = numeric(3),stringsAsFactors=FALSE)
> fixed[1, ] <- c("lunch", 100)
> str(fixed)
'data.frame': 3 obs. of 2 variables:
$ Type : chr "lunch" "" ""
$ Amount: chr "100" "0" "0"
CSV 파일에서 직접 읽는 경우 다음과 같이 하십시오.
myDataFrame <- read.csv("path/to/file.csv", header = TRUE, stringsAsFactors = FALSE)
다음은 모든 경우, 특히 다음과 같은 유연한 접근 방식입니다.
- 하나의 열에만 영향을 미치거나
- 그
dataframe
이전 작업(예: 파일을 즉시 열지 않거나 새 데이터 프레임을 만들지 않음)을 적용하여 얻은 데이터입니다.
먼저, 다음을 사용하여 문자열 인수 분해as.character
기능, 그리고 나서, 다시 인수분해합니다.as.factor
(또는 간단히)factor
) 함수:
fixed <- data.frame("Type" = character(3), "Amount" = numeric(3))
# Un-factorize (as.numeric can be use for numeric values)
# (as.vector can be use for objects - not tested)
fixed$Type <- as.character(fixed$Type)
fixed[1, ] <- c("lunch", 100)
# Re-factorize with the as.factor function or simple factor(fixed$Type)
fixed$Type <- as.factor(fixed$Type)
이 문제를 해결하는 가장 쉬운 방법은 열에 새 요인을 추가하는 것입니다.수준 함수를 사용하여 요인 수를 확인한 다음 새 요인을 추가합니다.
> levels(data$Fireplace.Qu)
[1] "Ex" "Fa" "Gd" "Po" "TA"
> levels(data$Fireplace.Qu) = c("Ex", "Fa", "Gd", "Po", "TA", "None")
[1] "Ex" "Fa" "Gd" "Po" " TA" "None"
.xlsx 파일에서 검색한 데이터와 유사한 문제가 있습니다.안타깝게도, 저는 여기서 적절한 답을 찾을 수 없었습니다.다른 사람들에게 도움이 될 수 있는 아래와 같이 dplyr로 스스로 처리했습니다.
#install.packages("xlsx")
library(xlsx)
extracted_df <- read.xlsx("test.xlsx", sheetName='Sheet1', stringsAsFactors=FALSE)
# Replace all NAs in a data frame with "G" character
extracted_df[is.na(extracted_df)] <- "G"
하지만, 저는 그것과 유사한 파라미터를 가지고 있지 않은 패키지로 그것을 처리할 수 없었습니다.stringsAsFactors
그 이유로, 나는 이사를 했습니다.xlsx
꾸러미
언급URL : https://stackoverflow.com/questions/16819956/warning-message-in-invalid-factor-level-na-generated
반응형
'programing' 카테고리의 다른 글
분기 인식 프로그래밍 (0) | 2023.06.14 |
---|---|
Wordpress URL의 슬래시 전달 (0) | 2023.06.14 |
axios 응답 값을 변수 vue.js에 할당할 수 없습니다. (0) | 2023.06.14 |
오류 Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKey토큰=31bf3856ad364e35 (0) | 2023.06.14 |
Tkinter에서 윈도우 닫기 이벤트는 어떻게 처리합니까? (0) | 2023.06.14 |