Hi I have doing the R-exercises to improve my R programming capabilities. ?Data.frame exercise 4 showed me that I have ?a language problem. ?Here's the problem and my "solution". # ?Exercise 4# ?Create a simple data frame from 3 vectors. Order the entire data frame by the# ?first column.df2 <- data.frame(a = 5:1,b = letters[1:5], c = runif(5))order(df2$a)?Naturally the order function did nothing. ? Per "help"Description order returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments. sort.list is the same, using only one argument.See the examples for how to use these functions to sort data frames, etc. Usage order(..., na.last = TRUE, decreasing = FALSE,? ? ? method = c("shell", "radix")) sort.list(x, partial = NULL, na.last = TRUE, decreasing = FALSE,? ? ? ? ? method = c("shell", "quick", "radix"))Arguments ... a sequence of numeric, complex, character or logical vectors, all of the same length, or a classed R object. Well, doesn't ... mean any legal object? ?I gave it a legal object and got nada.And the answer absolutely has me screaming "Say What"df2[with(df2,order(a)),] What's with "with? ?It is one function I do not use because I find it incomprehensible. ?To witEvaluate an R expression in an environment constructed from data, possibly modifying (a copy of) the original data. First of all, if I'm not modifying data (or as a subset activity creating data), why an I doing whatever it is I'm doing? ("possibly modifying (a copy of) the original data.") Evaluate. ?According to the thesarus A) assess(v), b) appraise, c) gage. OK, am I in a safe area? ?I'll evaluate that. ?Do I desire future social contact with this person? ?I'll evaluate that.In no way do I ever evaluate an equation. ?I may attempt to solve it. ?I may do a computer program to do the calculations and return a result. ?I will probably evaluate the result as to whether or not it helps solve the problem. ?Think in terms of an income tax return. ?But evaluate an R expression? ?No clue what that might mean. The remainder of the definition is also obtuse. ?an R expression in an environment constructed from data. ?Why would one make an environment without data? ?Obviously I am missing the point. ?My own created function makes a new environment, but I only created it to crunch numbers. ?If it doesn't crunch numbers it's useless. The point is, I do not understand the definition of "with" and thus have no idea how to use it. ?I guess computerese is analogous to taxlawese. ?Familiar words have entirely different meanings. Carl Sutton CPA [[alternative HTML version deleted]]
On 08/09/2016 6:57 PM, Carl Sutton via R-help wrote:> Hi > I have doing the R-exercises to improve my R programming capabilities. Data.frame exercise 4 showed me that I have a language problem. Here's the problem and my "solution". > # Exercise 4# Create a simple data frame from 3 vectors. Order the entire data frame by the# first column.df2 <- data.frame(a = 5:1,b = letters[1:5], c = runif(5))order(df2$a) Naturally the order function did nothing. > Per "help"Description > order returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments. sort.list is the same, using only one argument.See the examples for how to use these functions to sort data frames, etc. > Usage > order(..., na.last = TRUE, decreasing = FALSE, method = c("shell", "radix")) > sort.list(x, partial = NULL, na.last = TRUE, decreasing = FALSE, method = c("shell", "quick", "radix"))Arguments > ... a sequence of numeric, complex, character or logical vectors, all of the same length, or a classed R object. > Well, doesn't ... mean any legal object? I gave it a legal object and got nada.And the answer absolutely has me screaming "Say What"df2[with(df2,order(a)),] > > What's with "with? It is one function I do not use because I find it incomprehensible. To witEvaluate an R expression in an environment constructed from data, possibly modifying (a copy of) the original data. > > First of all, if I'm not modifying data (or as a subset activity creating data), why an I doing whatever it is I'm doing? ("possibly modifying (a copy of) the original data.") > Evaluate. According to the thesarus A) assess(v), b) appraise, c) gage. > OK, am I in a safe area? I'll evaluate that. Do I desire future social contact with this person? I'll evaluate that.In no way do I ever evaluate an equation. I may attempt to solve it. I may do a computer program to do the calculations and return a result. I will probably evaluate the result as to whether or not it helps solve the problem. Think in terms of an income tax return. But evaluate an R expression? No clue what that might mean. > The remainder of the definition is also obtuse. an R expression in an environment constructed from data. Why would one make an environment without data? Obviously I am missing the point. My own created function makes a new environment, but I only created it to crunch numbers. If it doesn't crunch numbers it's useless. > The point is, I do not understand the definition of "with" and thus have no idea how to use it. I guess computerese is analogous to taxlawese. Familiar words have entirely different meanings. > Carl Sutton CPA > > [[alternative HTML version deleted]]This is really hard to read, because you posted in HTML. If you don't get a useful answer, please try again in plain text. Duncan Murdoch
I echo Duncan's plea. But I can easily resolve one question: "What's with "with? It is one function I do not use because I find it incomprehensible. " Consider: ## first, clear the workspace, also known as the Global environment> rm(list=ls())## now create a data frame (or list or environment or...) containing objects named "x" and "w"> d <- data.frame(x=1:3,w=5:7)## now define a different "x" in the workspace> x <- 4:6 > > ## The following will produce an error, because there is no "w" in the workspace > ## > wError: object 'w' not found> > ## But this won't, since with() tells it's expression to first search in d. > > with(d,w)[1] 5 6 7> > ## similarly > > ##error > x+wError: object 'w' not found> > ## But > with(d, x+w)[1] 6 8 10> > ## In general, the second argument of d can be any expression that you could type at the console. > > ## If something can't be found in d, it will be looked for in d's "parent" environment, which is more involved than I want to get here. But: > > y <- 5 > > with(d, x+y) ## used x in d, and y in the workspace.[1] 6 7 8 HTH Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Sep 8, 2016 at 3:57 PM, Carl Sutton via R-help <r-help at r-project.org> wrote:> Hi > I have doing the R-exercises to improve my R programming capabilities. Data.frame exercise 4 showed me that I have a language problem. Here's the problem and my "solution". > # Exercise 4# Create a simple data frame from 3 vectors. Order the entire data frame by the# first column.df2 <- data.frame(a = 5:1,b = letters[1:5], c = runif(5))order(df2$a) Naturally the order function did nothing. > Per "help"Description > order returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments. sort.list is the same, using only one argument.See the examples for how to use these functions to sort data frames, etc. > Usage > order(..., na.last = TRUE, decreasing = FALSE, method = c("shell", "radix")) > sort.list(x, partial = NULL, na.last = TRUE, decreasing = FALSE, method = c("shell", "quick", "radix"))Arguments > ... a sequence of numeric, complex, character or logical vectors, all of the same length, or a classed R object. > Well, doesn't ... mean any legal object? I gave it a legal object and got nada.And the answer absolutely has me screaming "Say What"df2[with(df2,order(a)),] > > What's with "with? It is one function I do not use because I find it incomprehensible. To witEvaluate an R expression in an environment constructed from data, possibly modifying (a copy of) the original data. > > First of all, if I'm not modifying data (or as a subset activity creating data), why an I doing whatever it is I'm doing? ("possibly modifying (a copy of) the original data.") > Evaluate. According to the thesarus A) assess(v), b) appraise, c) gage. > OK, am I in a safe area? I'll evaluate that. Do I desire future social contact with this person? I'll evaluate that.In no way do I ever evaluate an equation. I may attempt to solve it. I may do a computer program to do the calculations and return a result. I will probably evaluate the result as to whether or not it helps solve the problem. Think in terms of an income tax return. But evaluate an R expression? No clue what that might mean. > The remainder of the definition is also obtuse. an R expression in an environment constructed from data. Why would one make an environment without data? Obviously I am missing the point. My own created function makes a new environment, but I only created it to crunch numbers. If it doesn't crunch numbers it's useless. > The point is, I do not understand the definition of "with" and thus have no idea how to use it. I guess computerese is analogous to taxlawese. Familiar words have entirely different meanings. > Carl Sutton CPA > > [[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.
Hi Carl, order vs sort The order function just returns the indices necessary to put the object into the sorted order, while the sort function returns the sorted object. If you want to use the order function: newdf2<-df2[(order(df2[,1]),] Yes, "with" can be a bit challenging. Think of it as: with(take_this_thing, and_do_this_with_it) The usual problem is working out what you want to do and what you want the function to return. It's probably best to just do things to data objects in a stepwise manner until you get used to that. Jim On Fri, Sep 9, 2016 at 9:07 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> On 08/09/2016 6:57 PM, Carl Sutton via R-help wrote: >> >> Hi >> I have doing the R-exercises to improve my R programming capabilities. >> Data.frame exercise 4 showed me that I have a language problem. Here's the >> problem and my "solution". >> # Exercise 4# Create a simple data frame from 3 vectors. Order the >> entire data frame by the# first column.df2 <- data.frame(a = 5:1,b >> letters[1:5], c = runif(5))order(df2$a) Naturally the order function did >> nothing. >> Per "help"Description >> order returns a permutation which rearranges its first argument into >> ascending or descending order, breaking ties by further arguments. sort.list >> is the same, using only one argument.See the examples for how to use these >> functions to sort data frames, etc. >> Usage >> order(..., na.last = TRUE, decreasing = FALSE, method = c("shell", >> "radix")) >> sort.list(x, partial = NULL, na.last = TRUE, decreasing = FALSE, >> method = c("shell", "quick", "radix"))Arguments >> ... a sequence of numeric, complex, character or logical vectors, all of >> the same length, or a classed R object. >> Well, doesn't ... mean any legal object? I gave it a legal object and got >> nada.And the answer absolutely has me screaming "Say >> What"df2[with(df2,order(a)),] >> >> What's with "with? It is one function I do not use because I find it >> incomprehensible. To witEvaluate an R expression in an environment >> constructed from data, possibly modifying (a copy of) the original data. >> >> First of all, if I'm not modifying data (or as a subset activity creating >> data), why an I doing whatever it is I'm doing? ("possibly modifying (a copy >> of) the original data.") >> Evaluate. According to the thesarus A) assess(v), b) appraise, c) gage. >> OK, am I in a safe area? I'll evaluate that. Do I desire future social >> contact with this person? I'll evaluate that.In no way do I ever evaluate >> an equation. I may attempt to solve it. I may do a computer program to do >> the calculations and return a result. I will probably evaluate the result >> as to whether or not it helps solve the problem. Think in terms of an >> income tax return. But evaluate an R expression? No clue what that might >> mean. >> The remainder of the definition is also obtuse. an R expression in an >> environment constructed from data. Why would one make an environment >> without data? Obviously I am missing the point. My own created function >> makes a new environment, but I only created it to crunch numbers. If it >> doesn't crunch numbers it's useless. >> The point is, I do not understand the definition of "with" and thus have >> no idea how to use it. I guess computerese is analogous to taxlawese. >> Familiar words have entirely different meanings. >> Carl Sutton CPA >> >> [[alternative HTML version deleted]] > > > This is really hard to read, because you posted in HTML. If you don't get a > useful answer, please try again in plain text. > > Duncan Murdoch > > > ______________________________________________ > 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.
You don't say where any of this code you are looking at came from, but I suspect [1]. If you feel the author of that site is failing to explain their answers sufficiently, please communicate that to them, not us. I agree that the documentation file for with() is rather opaque to a beginner and could be extended, but the jargon is referring to some valuable concepts that you should find a way to learn about (e.g. [2]). As Duncan pointed out, most of your diatribe was destroyed by your use of HTML format email, so if you can fix that problem and pose your questions calmly and with complete context in the email then someone might be interested in discussing them further with you. [1] http://r-exercises.com/ [2] http://r-adv.had.co.nz -- Sent from my phone. Please excuse my brevity. On September 8, 2016 4:07:54 PM PDT, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:>On 08/09/2016 6:57 PM, Carl Sutton via R-help wrote: >> Hi >> I have doing the R-exercises to improve my R programming >capabilities. Data.frame exercise 4 showed me that I have a language >problem. Here's the problem and my "solution". >> # Exercise 4# Create a simple data frame from 3 vectors. Order the >entire data frame by the# first column.df2 <- data.frame(a = 5:1,b >letters[1:5], c = runif(5))order(df2$a) Naturally the order function >did nothing. >> Per "help"Description >> order returns a permutation which rearranges its first argument into >ascending or descending order, breaking ties by further arguments. >sort.list is the same, using only one argument.See the examples for how >to use these functions to sort data frames, etc. >> Usage >> order(..., na.last = TRUE, decreasing = FALSE, method >c("shell", "radix")) >> sort.list(x, partial = NULL, na.last = TRUE, decreasing = FALSE, > method = c("shell", "quick", "radix"))Arguments >> ... a sequence of numeric, complex, character or logical vectors, all >of the same length, or a classed R object. >> Well, doesn't ... mean any legal object? I gave it a legal object >and got nada.And the answer absolutely has me screaming "Say >What"df2[with(df2,order(a)),] >> >> What's with "with? It is one function I do not use because I find it >incomprehensible. To witEvaluate an R expression in an environment >constructed from data, possibly modifying (a copy of) the original >data. >> >> First of all, if I'm not modifying data (or as a subset activity >creating data), why an I doing whatever it is I'm doing? ("possibly >modifying (a copy of) the original data.") >> Evaluate. According to the thesarus A) assess(v), b) appraise, c) >gage. >> OK, am I in a safe area? I'll evaluate that. Do I desire future >social contact with this person? I'll evaluate that.In no way do I >ever evaluate an equation. I may attempt to solve it. I may do a >computer program to do the calculations and return a result. I will >probably evaluate the result as to whether or not it helps solve the >problem. Think in terms of an income tax return. But evaluate an R >expression? No clue what that might mean. >> The remainder of the definition is also obtuse. an R expression in >an environment constructed from data. Why would one make an >environment without data? Obviously I am missing the point. My own >created function makes a new environment, but I only created it to >crunch numbers. If it doesn't crunch numbers it's useless. >> The point is, I do not understand the definition of "with" and thus >have no idea how to use it. I guess computerese is analogous to >taxlawese. Familiar words have entirely different meanings. >> Carl Sutton CPA >> >> [[alternative HTML version deleted]] > >This is really hard to read, because you posted in HTML. If you don't >get a useful answer, please try again in plain text. > >Duncan Murdoch > >______________________________________________ >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.