[R] Question about addressing a data frame

William Dunlap wdun|@p @end|ng |rom t|bco@com
Thu Apr 25 19:02:55 CEST 2019


is.na(DF) is a matrix for a data.frame DF.  The semantics of '[" are
different for matrices
and data.frame and that can cause confusion

> DF <- data.frame(X=c(101,NA,NA), Y=c("one","two",NA),
row.names=c("i","ii","iii"))
> is.na(DF) # returns a matrix when given a data.frame
        X     Y
i   FALSE FALSE
ii   TRUE FALSE
iii  TRUE  TRUE
> which(is.na(DF)) # returns a vector when given a data.frame
[1] 2 3 6
> which(is.na(DF), arr.ind=TRUE) # returns a length(dim(matrix))-column
matrix when given an array
    row col
ii    2   1
iii   3   1
iii   3   2
> DF[!is.na(DF)] # as.matrix(DF)[ !is.na(as.matrix(DF)) ]
[1] "101" "one" "two"

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Thu, Apr 25, 2019 at 9:27 AM Nitu, Laurentiu <
Laurentiu.Nitu using fraserhealth.ca> wrote:

> Hello,
>
>
> I have this data frame [algae] in the package DMwR. I thought I understand
> how to refer an element but I cannot explain...
>
> is.na(algae) is giving us the a logical vector with TRUE being the na's.
> which(is.na(algae)) gives the positions on the elements in the data frame
> where is.na returns TRUE.
>
>
> However which(is.na(algae)) returns
>
>
> [1]  648  838  862 1055 1056 1057 1058 1059 1060 1061 1062 1161 1199 1262
> 1399 1462 1599 1662 1799 1828 1999 2055 2056 2057 2058 2059 2060 2061 2062
> 2063
> [31] 2116 2184 2199
>
> Weirs since:
>
>
> > dim(algae)
>
> [1] 200  18
>
>
> If I refer back algae[which(is.na(algae))) I get a vector of NA's...
>
> What are the values returned by which(is.na(algae))?
>
> Thanks a lot for your help
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list