pearselab / r-intro-aspri951

r-intro-aspri951 created by GitHub Classroom
0 stars 1 forks source link

printing asterisks in R #3

Open aspri951 opened 7 years ago

aspri951 commented 7 years ago

I am also stuck on question 9 (drawing a box). My (smaller) goal: write code that returns a line of asterisks with no quotation marks around them. Problem, as I understand it: R views "*" as an operator (multiplication), so it returns an error whenever it sees an asterisk in a context that is NOT "multiplying numbers." Making the asterisk into a character fixes this, but means my line is full of quotation marks:

star.line <- function(w){
  top.of.box <- rep("*", w)
  return(top.of.box)
}
#works, but has quotations around the asterisks

star.line <- function(w){
  top.of.box <- "*"
  return(rep(top.of.box, w))
}
#same result. Try using print?

star.line <- function(w){
  for(i in 1:w){
    print("*")
  }
}
#prints vertical row of asterisks in quotations

Question: What R function would allow me to print an asterisk (or where would I look to find such a function)? I've looked through all the terms I defined in Assignment 1 and considered matrix, table, the par function bty, but none seem to be useful. Maybe I'm missing something.

willpearse commented 7 years ago

Try the "cat" function. Note that "\n" will print a new line.

On Sat, Jan 21, 2017, 12:26 AM aspri951 notifications@github.com wrote:

I am also stuck on question 9 (drawing a box). My (smaller) goal: write code that returns a line of asterisks with no quotation marks around them. Problem, as I understand it: R views "*" as an operator (multiplication), so it returns an error whenever it sees an asterisk in a context that is NOT "multiplying numbers." Making the asterisk into a character fixes this, but means my line is full of quotation marks:

star.line <- function(w){ top.of.box <- rep("*", w) return(top.of.box) }

works, but has quotations around the asterisks

star.line <- function(w){ top.of.box <- "*" return(rep(top.of.box, w)) }

same result. Try using print?

star.line <- function(w){ for(i in 1:w){ print("*") } }

prints vertical row of asterisks in quotations

Question: What R function would allow me to print an asterisk (or where would I look to find such a function)? I've looked through all the terms I defined in Assignment 1 and considered matrix, table, the par function bty, but none seem to be useful. Maybe I'm missing something.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pearselab/r-intro-aspri951/issues/3, or mute the thread https://github.com/notifications/unsubscribe-auth/ABLcUlLlkqakM5T795PxiIfHSVDWG9gSks5rUZb9gaJpZM4Lp-C7 .

aspri951 commented 7 years ago

I made a box! Took an hour, but if I could finish every problem in only an hour life would be good indeed...

Question: How can I go about trying to find a function I've never seen before (other than you just telling me)? In the future I might need a function that's not in assignment #1, and as I mentioned, it's these basic syntax/language fluency problems that keep getting me stuck, not the programming itself. Once I had the "cat" function, writing a function to draw a box was simple! Clearly what I'm lacking is a) a good resource for fixing my basic language/syntax ignorance, and/or b) the skill to sort through resources like stack overflow without getting myself into even deeper trouble due to my aforementioned language ignorance.

... I'm guessing there's not a short or easy answer to this question. Sorry! Either way, hopefully it's useful to know where I'm continually getting stuck.

willpearse commented 7 years ago

Mostly via Google, to be quite honest with you... Otherwise searching by typing "??print" or ??"confusing thing" (notice my double quotes in the last example when searching for something with a space in it) at the console.

For what it's worth, it is possible to complete the exercise without cat, but I realise that's probably of little consolation :(

On Sun, Jan 22, 2017, 5:25 PM aspri951 notifications@github.com wrote:

I made a box! Took an hour, but if I could finish every problem in only an hour life would be good indeed...

Question: How can I go about trying to find a function I've never seen before (other than you just telling me)? In the future I might need a function that's not in assignment #1 https://github.com/pearselab/r-intro-aspri951/issues/1, and as I mentioned, it's these basic syntax/language fluency problems that keep getting me stuck, not the programming itself. Once I had the "cat" function, writing a function to draw a box was simple! Clearly what I'm lacking is a) a good resource for fixing my basic language/syntax ignorance, and/or b) the skill to sort through resources like stack overflow without getting myself into even deeper trouble due to my aforementioned language ignorance.

... I'm guessing there's not a short or easy answer to this question. Sorry! Either way, hopefully it's useful to know where I'm continually getting stuck.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/pearselab/r-intro-aspri951/issues/3#issuecomment-274372793, or mute the thread https://github.com/notifications/unsubscribe-auth/ABLcUhxg4bxlCaIjcfi8M6ve9NbGpGLtks5rU_OIgaJpZM4Lp-C7 .