簡體   English   中英

na.omit 函數不會刪除包含 NA 的行

[英]na.omit function is not removing rows containing NA

嗨,我在互聯網上查看出了什么問題, na.omit()函數沒有刪除帶有 NA 的行。 請你幫助我好嗎?

library(TTR)
library(quantmod)
library(doParallel) #this library is for parallel core processing

StartDate = "2010-01-01"
EndDate = "2020-03-20"
myStock <- c("AMZN")
getSymbols(myStock, src="yahoo", from=StartDate, to=EndDate)

gdat <-coredata(AMZN$AMZN.Close) # Create a 2-d array of all the data. Or...
Data <- data.frame(date=index(AMZN), coredata(AMZN)) # Create a data frame with the data and (optionally) maintain the date as an index

Data$rsi22 <- data.frame(RSI(Cl(Data), n=22))
Data$rsi44 <- data.frame(RSI(Cl(Data), n=44))
colnames(Data)

DatanoNA <- na.omit(Data) #remove rows with NAs

我認為您正在尋找complete.cases()函數。 na.omit()用於刪除向量中的NA值,而不是用於從數據框中刪除包含NA值的行。

此外,您的數據框構造有點不穩定(有關更多解釋,請參見下文)。 嘗試這個:

Data <- data.frame(date=index(AMZN), coredata(AMZN),
                  rsi22=RSI(Cl(Data), n=22),
                  rsi44=RSI(Cl(Data), n=44))
nrow(Data)
nrow(Data[complete.cases(Data),])

通常,數據幀的每一列都是一個向量。 RSI()的結果存儲為向量。 當你說

Data$rsi22 <- data.frame(RSI(Cl(Data), n=22))

您正在做的是將結果包裝在一個數據框中,然后將其嵌入另一個數據框( Data ),這是您可以在 R 中合法執行的操作,但這是不尋常的,並且會混淆許多標准數據處理功能。

你可以試試complete.cases

DatanoNA <- Data[complete.cases(Data),]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM