Bert Gunter
2022-Oct-28 14:25 UTC
[R] Unexpected result for df column $ subset with non-existent name
Does this explain it: (from ?Extract) name A literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under ?Environments?) **partially matched** to the names of the object. -- Bert On Fri, Oct 28, 2022 at 6:53 AM Sergei Ko <sggp.sergei at gmail.com> wrote:> > Hi All, > > Just noticed that R returns results for non-existent name if you have > another variable with the same beginning when you subset with $. > See the code below: > name_0 <- "ID" > name_1 <- "name" > name_2 <- "name1" > > v0 <- 1:200 > v1 <- c(rep(0,100), rep(1,100)) > v2 <- c(rep(0,50), rep(1,150)) > > df <- as.data.frame(cbind(v0, v1, v2)) > colnames(df) <- c(name_0, name_1, name_2) > > df_1 <- df[, c(name_0, name_1)] > df_2 <- df[, c(name_0, name_2)] > > table(df$name) > table(df_1$name) > table(df_2$name) > colnames(df_2)[2] <- "name10" > table(df_2$name) > colnames(df_2)[2] <- "name_any" > table(df_2$name) > table(df_2[,"name"]) > > The last row produces an error as intended. > Any ideas? > > Win 10 > R version 4.2.1 (2022-06-23 ucrt) -- "Funny-Looking Kid" > > Regards, > Sergiy > > [[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.
Joshua Ulrich
2022-Oct-30 16:54 UTC
[R] Unexpected result for df column $ subset with non-existent name
For what it's worth, I set these options to warn me about partial matches: options(warnPartialMatchArgs = TRUE, warnPartialMatchAttr = TRUE, warnPartialMatchDollar = TRUE) That warns about this particular case, as well as partial argument name matches in function calls. I don't remember what the partial match attr does, but past me wanted to be warned about it for some reason. Best, Josh On Fri, Oct 28, 2022 at 9:26 AM Bert Gunter <bgunter.4567 at gmail.com> wrote:> > Does this explain it: (from ?Extract) > > > name > A literal character string or a name (possibly backtick quoted). For > extraction, this is normally (see under ?Environments?) **partially > matched** to the names of the object. > > -- Bert > > On Fri, Oct 28, 2022 at 6:53 AM Sergei Ko <sggp.sergei at gmail.com> wrote: > > > > Hi All, > > > > Just noticed that R returns results for non-existent name if you have > > another variable with the same beginning when you subset with $. > > See the code below: > > name_0 <- "ID" > > name_1 <- "name" > > name_2 <- "name1" > > > > v0 <- 1:200 > > v1 <- c(rep(0,100), rep(1,100)) > > v2 <- c(rep(0,50), rep(1,150)) > > > > df <- as.data.frame(cbind(v0, v1, v2)) > > colnames(df) <- c(name_0, name_1, name_2) > > > > df_1 <- df[, c(name_0, name_1)] > > df_2 <- df[, c(name_0, name_2)] > > > > table(df$name) > > table(df_1$name) > > table(df_2$name) > > colnames(df_2)[2] <- "name10" > > table(df_2$name) > > colnames(df_2)[2] <- "name_any" > > table(df_2$name) > > table(df_2[,"name"]) > > > > The last row produces an error as intended. > > Any ideas? > > > > Win 10 > > R version 4.2.1 (2022-06-23 ucrt) -- "Funny-Looking Kid" > > > > Regards, > > Sergiy > > > > [[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. > > ______________________________________________ > 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.-- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com