Rasmus Liland
2023-Mar-16 12:42 UTC
[R] Trying to learn how to write an "advanced" function
On 2023-03-16 12:11 +0000, Sorkin, John wrote:> (1) can someone point me to an > explanation of match.call or match > that can be understood by the > uninitiated?Dear John, the man page ?match tells us that match matches the first vector against the second, and returns a vector of indecies the same length as the first, e.g. > match(c("formula", "data", "subset", "weights", "na.action", "offset"), c("Maryland", "formula", "data", "subset", "weights", "na.action", "offset", "Sorkin", "subset"), 0L) [1] 2 3 4 5 6 7 perhaps a bad answer ...> (2) can someone point me to a document > that will help me learn how to write > an "advanced" function?Perhaps the background here is looking at the lm function as a basis for writing something more advanced, then the exercise becomes looking at dput(lm), understanding every line by looking up all the functions you do not understand in the man pages e.g. ?match. Remember, you can search for things inside R by using double questionmark, ??match, finding versions of match existing inside other installed packages, e.g. raster::match and posterior::match, perhaps this exercise becomes writing ones own version of lm inside ones own package? Best, Rasmus
Sorkin, John
2023-Mar-16 14:53 UTC
[R] Trying to learn how to write an "advanced" function
Although I owe thanks to Ramus and Ivan, I still do not know how to write and "advanced" function. My most recent try (after looking at the material Ramus and Ivan set) still does not work. I am trying to run the lm function on two different formulae: 1) y~x, 2) y~x+z Any corrections would be appreciated! Thank you, John doit <- function(x){ ds <- deparse(substitute(x)) cat("1\n") print(ds) eval(lm(quote(ds)),parent.frame()) } # define data that will be used in regression y <- 1:10 x <- y+rnorm(10) z <- c(rep(1,5),rep(2,5)) # Show what x, y and z look like rbind(x,y,z) # run formula y~x JD <- doit(y~x) JD # run formula y~x+z JD2 <- doit(y~x+z) JD2 ________________________________________ From: R-help <r-help-bounces at r-project.org> on behalf of Rasmus Liland <jral at posteo.no> Sent: Thursday, March 16, 2023 8:42 AM To: r-help Subject: Re: [R] Trying to learn how to write an "advanced" function On 2023-03-16 12:11 +0000, Sorkin, John wrote:> (1) can someone point me to an > explanation of match.call or match > that can be understood by the > uninitiated?Dear John, the man page ?match tells us that match matches the first vector against the second, and returns a vector of indecies the same length as the first, e.g. > match(c("formula", "data", "subset", "weights", "na.action", "offset"), c("Maryland", "formula", "data", "subset", "weights", "na.action", "offset", "Sorkin", "subset"), 0L) [1] 2 3 4 5 6 7 perhaps a bad answer ...> (2) can someone point me to a document > that will help me learn how to write > an "advanced" function?Perhaps the background here is looking at the lm function as a basis for writing something more advanced, then the exercise becomes looking at dput(lm), understanding every line by looking up all the functions you do not understand in the man pages e.g. ?match. Remember, you can search for things inside R by using double questionmark, ??match, finding versions of match existing inside other installed packages, e.g. raster::match and posterior::match, perhaps this exercise becomes writing ones own version of lm inside ones own package? Best, Rasmus ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7CJSorkin%40som.umaryland.edu%7C8e0b6e6627474ceceed608db261ca383%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638145676673285449%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=coJy2i9Nj%2Fs23ElOAM7kaYpTTBSKDo5B557tNf2twSA%3D&reserved=0 PLEASE do read the posting guide https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7CJSorkin%40som.umaryland.edu%7C8e0b6e6627474ceceed608db261ca383%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638145676673285449%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=eByabBUy5c7zdefrSSq3xbgjMTcsxwbBGD33lTwv4Pg%3D&reserved=0 and provide commented, minimal, self-contained, reproducible code.