简体   繁体   中英

Error:: is.na() applied to non-(list or vector) of type 'NULL'

When I am using below code then it gives me the desired output

subset(ndata,!is.na(ndata$LHeart_Failure))

While when I did the above same code in other form

out <- "LHeart_Failure"
subset(ndata,!is.na(ndata$out))

it is giving me error:

logical(0)
Warning message:
In is.na(ndata$out) :
is.na() applied to non-(list or vector) of type 'NULL'

Here ndata is the data from the .csv file. LHeart_Failure is the name of the column, by which I want to find the all the rows in which there is no "NA" in that particular column(ie all the rows in which there is no NA in LHeart_Failure).

I get this error message when I try to call is.na() on a data frame column which does not exist, eg

> ndata <- data.frame(v1=c(1:3), v2=c("one", "two", "three"))
> subset(ndata, !is.na(ndata$out))
[1] v1 v2
<0 rows> (or 0-length row.names)
Warning message:
In is.na(ndata$out) :
  is.na() applied to non-(list or vector) of type 'NULL'

So you should make sure that the column out actually exists first before trying to subset using it.

I would personally avoid using subset and instead just subset the data frame explicitly. From what you wrote, it looks like you want to retain rows from the data frame ndata where the out column does not have an NA value. In this case, you can try:

ndata[!is.na(ndata$out), ]

The error is because the data structure ndata does not contain any column called out . With the command:

out <- "LHeart_Failure"

you only create a variable out with the string content "LHeart_Failure" . If you want to use out as a equivalent to the LHeart_Failure column, you have to call:

out <- ndata$LHeart_Failure

followed by

found_idxs <- which[!is.na(out)]

ndata$out assumes that the column out exists in the data ndata . If you want to use the string you've saved in out as the column name to filter the data, I suggest you do this:

out <- "LHeart_Failure"
ndata[!is.na(ndata[,out]),]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM