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]]
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.frameX 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))-columnmatrix 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 at 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 at 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]]
Thank you very much for your answer. Yes, meanwhile I found out ? Plus, that index that which() is returning applied on the matrix is calculated as: matrix[rowIndices + nrow(matrix) * (colIndices - 1)] thanks again for your help From: William Dunlap <wdunlap at tibco.com> Sent: Thursday, April 25, 2019 10:03 AM To: Nitu, Laurentiu <Laurentiu.Nitu at fraserhealth.ca> Cc: r-help at r-project.org Subject: Re: [R] Question about addressing a data frame is.na<http://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<http://is.na>(DF) # returns a matrix when given a data.frameX Y i FALSE FALSE ii TRUE FALSE iii TRUE TRUE> which(is.na<http://is.na>(DF)) # returns a vector when given a data.frame[1] 2 3 6> which(is.na<http://is.na>(DF), arr.ind=TRUE) # returns a length(dim(matrix))-column matrix when given an arrayrow col ii 2 1 iii 3 1 iii 3 2> DF[!is.na<http://is.na>(DF)] # as.matrix(DF)[ !is.na<http://is.na>(as.matrix(DF)) ][1] "101" "one" "two" Bill Dunlap TIBCO Software wdunlap tibco.com<http://tibco.com> On Thu, Apr 25, 2019 at 9:27 AM Nitu, Laurentiu <Laurentiu.Nitu at fraserhealth.ca<mailto:Laurentiu.Nitu at 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<http://is.na>(algae) is giving us the a logical vector with TRUE being the na's. which(is.na<http://is.na>(algae)) gives the positions on the elements in the data frame where is.na<http://is.na> returns TRUE. However which(is.na<http://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<http://is.na>(algae))) I get a vector of NA's... What are the values returned by which(is.na<http://is.na>(algae))? Thanks a lot for your help [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org<mailto:R-help at 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]]