'\r' is the carriage return character. The main times it would be useful are:
When reading text in binary mode, or which may come from a foreign OS, you'll find (and probably want to discard) it due to CR/LF line-endings from Windows-format text files.
When writing to an interactive terminal on
stdoutorstderr,'\r'can be used to move the cursor back to the beginning of the line, to overwrite it with new contents. This makes a nice primitive progress indicator.
The example code in your post is definitely a wrong way to use '\r'. It assumes a carriage return will precede the newline character at the end of a line entered, which is non-portable and only true on Windows. Instead the code should look for '\n' (newline), and discard any carriage return it finds before the newline. Or, it could use text mode and have the C library handle the translation (but text mode is ugly and probably should not be used).
Hi guys, i'm just starting out with R for uni. I understand most of it but its still not clear to me what the code c() actually does and when to use it. The file i got from school says it's to group some numbers together or something, it's a little confusing to me.
Videos
'\r' is the carriage return character. The main times it would be useful are:
When reading text in binary mode, or which may come from a foreign OS, you'll find (and probably want to discard) it due to CR/LF line-endings from Windows-format text files.
When writing to an interactive terminal on
stdoutorstderr,'\r'can be used to move the cursor back to the beginning of the line, to overwrite it with new contents. This makes a nice primitive progress indicator.
The example code in your post is definitely a wrong way to use '\r'. It assumes a carriage return will precede the newline character at the end of a line entered, which is non-portable and only true on Windows. Instead the code should look for '\n' (newline), and discard any carriage return it finds before the newline. Or, it could use text mode and have the C library handle the translation (but text mode is ugly and probably should not be used).
It's Carriage Return. Source: http://msdn.microsoft.com/en-us/library/6aw8xdf2(v=vs.80).aspx
The following repeats the loop until the user has pressed the Return key.
while(ch!='\r')
{
ch=getche();
}
In R, the c() function returns a vector (a one dimensional array).
In your example:
k <- c(0.5, 1) # k is a vector
k[1] # is 0.5 (remember, R indices start on 1)
k[2] # is 1
If you want to create a vector with 1024 entries (assuming 0.5 increments), you have at least two ways to do it:
# One way
k <- (1:1024) / 2 # this will be 0.5, 1, 1.5, 2, ... , 512
# Another way:
k <- seq(0.5, 512, 0.5)
Also you can use c() to concatenate two vectors:
k <- c(0.5, 1) # k = 0.5, 1
k <- c(k, 1.5) # k = 0.5, 1, 1.5
k <- c(k, c(2, 2.5)) # k = 0.5, 1, 1.5, 2, 2.5
k <- c(k, k) # k = 0.5, 1, 1.5, 2, 2.5, 0.5, 1, 1.5, 2, 2.5
Please check the help for c() and seq function (in R: ?c and ?seq)
Reference:
- Quick-R: Data types
The c function in R is used to create a vector with values you provide explicitly. If you want a sequence of values you can use the : operator. For example,
k <- 1:1024
gives you a vector with 1024 values.
The built-in help in R is pretty good for questions like this. Just type ?functionName in your R terminal to get help on any function loaded in your environment.
The characters will get send just like that to the underlying output device (in your case probably a terminal emulator).
It is up to the terminal's implementation then how those characters get actually displayed. For example, a bell (\a) could trigger a beep sound on some terminals, a flash of the screen on others, or it will be completely ignored. It all depends on how the terminal is configured.
The characters are exactly as documented - \b equates to a character code of 0x08 and \r equates to 0x0d. The thing that varies is how your OS reacts to those characters. Back when displays were trying to emulate an old teletype those actions were standardized, but they are less useful in modern environments and compatibility is not guaranteed.