Datacamp: String Manipulation Flashcards
Whe you have both ‘ and “ in string , how do you treat them?
there are cases where you need both ‘ and “ inside the string. In this case, fall back to the first guideline and use “ to define the string, but you’ll have to escape any double quotes inside the string using a backslash (i.e. ").
how to enclose string in ?
Both double quotes (“) and single (‘) quotes work, but there are some guidelines for which to use.
First, you should prefer double quotes (“) to single quotes (‘). That means, whenever you are defining a string your first intuition should be to use “
what about if you have “” in your text?
Unfortunately if your string has “ inside it, R will interpret the double quote as “this is the end of the string”, not as “this is the character “”. This is one time you can forget the first guideline and use the single quote, ‘, to define the string.
When you ask R for line2 it is actually calling print(line2) and the print() method for strings displays strings as you might enter them(even with escap e.g ‘\ “isa musa” ‘).
print() will print ‘\ “isa musa” ‘. How to adress the issue?
If you want to see the string it represents you’ll need to use a different function: writeLines().
What is the diff btween writeLines() and cat() function?
The function cat() is very similar to writeLines(), but by default separates elements with a space, and will attempt to convert non-character objects to a string. We won’t use it in this course, but you might see it in other people’s code.
escape sequence examPl?
" is used to denote a double quote.
In “hello\n\U1F30D” there are two escape sequences: \n gives a newline, and \U followed by up to 8 hex digits sequence denotes a particular Unicode character.
how to escpe backslash?
When R comes across a \ it assumes you are starting an escape, so if you actually need a backslash in your string you’ll need the sequence \.
how to show double backslash?
\\
What format() does and what are its argument?
decide whether the numbers are displayed in fixed (scientific = FALSE).. or scientific (scientific = TRUE) format….kamar 2.8-e10
format(x, digit=2)
formatc(x, format=f, digit=2, )
format(x, scientific = TRUE)
formatc(x , format =f/e/g ) f means fixed , e scientifc and g means scientific as well
What is fixed format and scientific format?
346 if fixed format 0.3 * 10 power 2 is scientific f
When numbers are long it can be helpful to “prettify” them, for example instead of 1000000000 display 1,000,000,000. with which function can you do this?
In this case a , is added every 3 digits. This can be controlled by the big.interval and big.mark arguments, e.g. format(1000000000, big.mark = “,”, big.interval = 3, scientific = FALSE)
What trim= TRUE/FALSE argument in format() does?
Not only does format() control the way the number is represented, it also controls some of the properties of the resulting string that affect its display.
For example, by default format() will pad the start of the strings with spaces so that the decimal points line up, which is really useful if you are presenting the numbers in a vertical column. However, if you are putting the number in the middle of a sentence, you might not want these extra spaces. You can set trim = TRUE to remove them.
what format() does ?
The function formatC() provides an alternative way to format numbers based on C style syntax.
Rather than a scientific argument, formatC() has a format argument that takes a code representing the required format. The most useful are:
“f” for fixed,
“e” for scientific, and
“g” for fixed unless scientific saves space