4 ms·
I was a mathematics major in college, and didn't have much training in programming when I graduated. R was the first language I learned when I started my career
by CapmCrackaWaka 5y ago
I was a mathematics major in college, and didn't have much training in programming when I graduated. R was the first language I learned when I started my career as an actuary, and it was a breeze. Things “just work”. Want to add 2 vectors of different dimensions together? R knows what you’re getting at, and makes it work. Comparatively, learning Python was harder.
Now that I’m used to both languages, I find it funny how much R is hated by “true” programmers.
- vharuck 5y agoThis is the key. R shouldn't be seen as a general programming language, but a domain specific language that's still open-ended. I started with SAS in my job, which was fine for statistics and handling tables. But anything beyond that, even supposedly simple things like reusing code or listing all files in a folder, was not simple. With R, it was. R only had to be ergonomically better than the competition, and they weren't very good.
- blt 5y agoWhat are you "getting at" by adding two vectors of different dimensions? It's not obvious to me. Off-by-one dimensionality errors are so common in programming. If the language does something like zero-extending instead of raising an error, it will lead to an "it runs but gives the wrong answer" bug. These are much more painful in numerical code than in logic-based code.
- CapmCrackaWaka 5y agoFor instance, if you wanted to add 1 to every other element in the vector (1,2,3,4,5,6). To someone who has no programming experience, this may be a daunting task. But simply doing (1,0) + (1,2,3,4,5,6) works in R.
- Closi 5y agoThe only problem with this is that I don't personally find it obvious/intuitive that (1,0) + (1,2,3,4,5,6) = (2,2,4,4,6,6). In fact, I would probably expect the output to be (1,0,1,2,3,4,5,6).
- CapmCrackaWaka 5y agoI guess that's the difference between people who prefer R vs. people who use more traditional languages. '+' means addition in the scientific world, if I'm trying to figure out how to add numbers the first thing I try is '+'. To me, those vectors are just that - vectors of numbers, not instances of a class (even though that is kind of true under the hood). I have a problem I want to solve, and R does a good job of giving me what I want.
- Karrot_Kream 5y agoIndeed, no mathematician or statistician would think of `+` as concatenation. List concatenation isn't a relevant problem to most mathematicians. This is where the above comments about prior knowledge and context come into play. Likewise, when I multiply two vectors together, or multiply a scalar with a vector, I have a definite idea what I "want" out of that multiplication. For many programmers, they think of this more as an exercise in data-types than an expression of linear algebra.
- Closi 5y agoStatisticians might see it differently, but I still don’t think it’s obvious that (1,0) represents effectively a repeating pattern that’s extended to the size of the other vector. For instance, for the statement (a,b,c,d,e) + (a,b,c,d,e,f) are we really all saying that it’s clear, obvious and unambiguous that this means (a+a, b+b, c+c, d+d, e+e, f+a). Personally if you showed me that and asked me to describe what would happen before reading this thread, I would have said it would either throw an error, concatenate the vectors or only add the matching elements. I wouldn’t personally guess that the first vector would be treated as a repeating sequence, but different people might make different assumptions - I just don’t think it’s particularly clear, and I struggle to believe it’s clear, unambiguous and obvious to statisticians too.
- Karrot_Kream 5y ago> I would have said it would either throw an error, concatenate the vectors or only add the matching elements Heh this itself is common evidence of an (experienced) programmer's way of thinking. Remember, when dealing with mathematics, there's no machine (or runtime or similar abstraction) there lurking in the background, enforcing conditions, or even lending a physical reality. Operations in mathematics are defined. Notation in mathematics is just an operation encoded as an "arbitrary" symbol. Which leads to > I just don’t think it’s particularly clear, and I struggle to believe it’s clear, unambiguous and obvious to statisticians too. This happens to be convention in both a subset of textbooks and many programming environments. It's mostly an artifact of the notation. If you want to explore what math notation "feels like" a bit more without learning math (which I _wholeheartedly_ recommend as it's incredibly useful), try out the APL programming language a bit. It evokes a similar atmosphere of notation conveying the idea of well-defined operations.
- roenxi 5y agoIn what branch of maths or science is (1,0) + (1,2,3,4,5,6) even defined? That operation shouldn't make sense to anyone; it is adding vectors of different dimensions. The result should probably be vector promotion then (2,2,3,4,5,6). (2,2,4,4,6,6) is not a good answer.
- CapmCrackaWaka 5y agoSure we can argue about the syntax and what ‘+’ should do but the point is that many people find the build in behavior of R to be intuitive and easy to learn, myself included.
- roenxi 5y agoHow can something be intuitive if it maps to a totally arbitrary abstract concept? Does that operation even have a name? That is the plus symbol and it is in the context of two vectors. The intuitive thing to do is to fail with an error if it is a mathematics operation, concatenate if it is a programming context or treat the two vectors as the same length by appending 0s to the shorter one if the goal is to be unhelpfully helpful. Repeating a vector until the dimensions match then element-wise adding them may be convenient for you, and you may like it. Maybe even lots of people like it. But it is a tough sell for me to believe it is intuitive.
- CapmCrackaWaka 5y ago> Does that operation even have a name. numpy calls it 'broadcasting', although this particular operation doesn't work. For example, this is valid in numpy: `np.array([1,2]) > np.array([[1],[2]])` The idea is that an attempt is made to handle the operation even if the arrays / matrices are incompatible. Extending the operations of matrices and vectors in this way allows for extremely concise operations that would otherwise be a pain, like in the original R example.
- roenxi 5y agoPretty sure it isn't broadcasting, because > c(1, 0) + c(1, 2, 3, 4, 5, 6) [1] 2 2 4 4 6 6 succeeds but in numpy >>> numpy.array([1,0]) + numpy.array([1, 2, 3, 4, 5, 6]) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: operands could not be broadcast together with shapes (2,) (6,) ie, numpy correctly recognises that totally incompatible dimensions can't be added.