4

I'm a statistics and math student, first year of my BA. I'm taking the class "Introduction to computer science for statistics students" where we learn R programming.

This is my first time to really go into programming, and I'm super frustrated. A lot of the homework tasks are essentially to write quite simple functions, and the task is "to understand the syntax" which is more like "to guess the syntax".

I'm trying to figure out how to find good and informative examples for using R - when the purpose is not to use it as a statistics tool - but to write simple code lines.

Unfortunately, this is much harder then it seems, because nobody is using R in this way.

For example, I need to write a function that will count the number of digits for some vector, assuming the vector has only one element:

x <- r

where r is a real number.

The function must use a while loop.

I don't know how to define the loop such that it will stop, when "achieving the goal" of counting the number of digits. I was thinking of:

count_digits <- function(x){
    y <- 1
    if(while(x %/% 10 == 0)) {
        return(y)
    }

The problem is I can't understand the "rules" of the 'while' loop. I haven't managed to find sufficient results when searching Stack Overflow, and cheat sheets, R syntax loops, or Coursera online course for R programming aren't helpful.

How can I find answers for my questions - without posting them and getting blocked - because most of the time my problem is only to figure out how to "sew" the pieces together?

20
  • 18
    If you don't even know how to get started on your homework then that sounds like something to bring to your teacher.
    – Servy
    Dec 3, 2018 at 15:39
  • 16
    If you've read through the entirety of the course material given to you, as well as doing outside research, and you still can't figure out how to do your homework, that's also something you should be bringing to your teacher.
    – Servy
    Dec 3, 2018 at 15:45
  • 3
    @Jneven I would say Servy is right, speak to your teacher about what you don't understand and they should be able to point you in the right direction. I can sympathise with your issue, you can't find what you want in any resource because you don't really know what you're looking for. FYI if I read correctly, you don't understand the operator in the while loop, understandably this can be hard to search for if you don't know what it's called
    – George
    Dec 3, 2018 at 15:46
  • 7
    Exactly. A lot of people make the mistake of trying to replace their teacher with Stack Overflow.
    – user247702
    Dec 3, 2018 at 15:46
  • 1
    @Jneven By using a search engine. There's lots of places on the internet with published code in basically every language.
    – Servy
    Dec 3, 2018 at 15:52
  • rexamples.com (not endorsing it, just the first result of a web search; name seemed quite appropriate).
    – yivi
    Dec 3, 2018 at 15:53
  • Found this on google: cran.r-project.org/doc/manuals/r-release/R-lang.html
    – user4639281
    Dec 3, 2018 at 15:55
  • 5
    @Jneven: Well, considering that you condescended to someone who is trying to give you genuinely helpful advice, I don't think it is reasonable to expect something other than downvotes. If you're just looking for examples of R, and you're not finding "helpful results" with a search engine, then it's not clear to us what "helpful results" would look like. So explain why Google is lacking (other than the fact that it is not giving you the exact answer to your homework). Dec 3, 2018 at 17:27
  • 1
    @NicolBolas my "condescended" comment was a reaction to the comments suggesting me talk to my prof, google or the internet. the question was down voted before my comments - which I'm honestly confused about. I wouldn't try Meta - Stack Overflow if I'd want online mocking :)
    – Noa Even
    Dec 3, 2018 at 17:39
  • 11
    @Jneven: Why is people suggesting you talk to your professor "mocking"? That sounds like very good advice for how to ask better questions: get your professor to give you a better foundation in the basics, so you won't have to ask basic questions. And you still haven't explained why the Internet is lacking the information you're looking for. Unless you're saying that the only way you can understand something is to see code of it. In which case, I would strongly suggest you learn to be able to read descriptions of how things work and transform them into working code. Dec 3, 2018 at 17:42
  • 4
    Why is talking to your instructor helpful, while searching Google is not? They're both pieces of information that can provide additional understanding. There are going to be things that you need to do, and unless you tell us you have, we're going to assume you haven't; most users won't, so it's reasonable to want to know what you've done beforehand.
    – fbueckert
    Dec 3, 2018 at 17:44
  • 4
    It's as genuine as recommending you talk to your professor. I honestly can't see where you said you've already used a search engine; all I see is, "Because nobody is using R at this format." Is that what you mean?
    – fbueckert
    Dec 3, 2018 at 17:50
  • 4
    You can read it as, "I searched, and found nothing helpful." You can also read it as, "I didn't search, because nobody uses this obscure language." Based on the number of users we get that do the latter, it's pretty reasonable to make that the basic assumption. That's how you get comments recommending you search for it. We don't know you did so unless you tell us that. Still not sure how that's different than asking your prof; we're still assuming you haven't asked them, either.
    – fbueckert
    Dec 3, 2018 at 17:56
  • 6
    What you need more than anything else is a good book and a compiler. Try stuff as you read. If your current book doesn't make sense and you have a hard time following the examples, then try another book. Internet tutorials may be an option, but then you have to us lots of scepticism, as many tutorials out there are bad. Books generally have higher quality standards.
    – Lundin
    Dec 4, 2018 at 8:02
  • 1
    @Lundin amen. My view on tutorials is that they tend to be written as a showcase, not a quality source of information. Good to know what can be done, not good to know and understand how it can be done. That makes them mostly interesting for people who already have experience and need a kick in the right direction or a memory boost.
    – Gimby
    Dec 4, 2018 at 8:50

2 Answers 2

12

You're right that R is not commonly used in the same way that most general-purpose programming languages are. I've seen many resources that explicitly warn against using loops at all, preferring to apply functions to rows in a dataframe instead. This can make it difficult to find examples that go "against the grain" and do things in the non-idiomatic (for R) way.

As mentioned in the comments, the very best resources are going to be your professor and your course materials. However, if you've already exhausted these resources, and you've done your research online, then you really should have enough information to ask a good question on Stack Overflow. By posting your question, what you've learned in your research, and what code you've tried so far, you'll be helping to create a resource for other students like you who are struggling to write R code in a non-idiomatic way.

22

I want to add a bit more to the answer that Bill provided, specifically this block:

However, if you've already exhausted these resources, and you've done your research online, then you really should have enough information to ask a good question on Stack Overflow.

I want to clarify what we generally mean by "exhausting" those resources.

  • When it comes to exhausting the search engine...
    • It doesn't mean that you've Googled for hours. You can spend hours on the Internet anyway and not make headway.
    • It does mean that you've looked at a few specific articles which you can reference in your question as points of discussion. Here's an example; in that question I explicitly list the reference to the mailing list as the key source of, "Okay, what do I do now?"
  • When it comes to exhausting person resources (classmates, coworkers, instructors or tutors)...
    • It doesn't mean that you've asked so many different people and they don't have an answer.
    • It does mean that you can surmise what they've stated thus far and use that as the basis or frame of your question. An example would be along the lines of, "My professor said X, but the tutors are stating that X is no longer used in the industry, but I do have to solve the problem with X."

The most important thing when it comes to "doing your research" isn't actually doing the research. It's proving that you've done the research. If you can provide poignant and exact references to what you've done as well as where you're stuck, then that would be a far better question than, "I've searched for hours and I don't know what to do."

1
  • 10
    Explaining "exhausting" is exhausting and this is a great answer to point to.
    – user50049
    Dec 3, 2018 at 18:20

Not the answer you're looking for? Browse other questions tagged .