Home > OS >  Usage of double quotes (" ") in R
Usage of double quotes (" ") in R

Time:02-21

I am a complete beginner in R and right now I am learning to use the fundamentals. I was wondering what the function of double quotes ("") is in R: when do you have to use it and when not? For example: when use library() you need them, however when using a function such as glimpse() to quickly explore a dataset, the double qoutes ("") actually interfere with the function of glimpse(); you do not see the dataset when include double qoutes.

TLDR: I do not know what double qoutes ("") actually do in R and I cannot find a beginner level explanation for it other than "it creates a string".

Thanks in advance!

CodePudding user response:

Well, its a bit complicated, but the simplest answer is that it is typically used to denote a character (letters, etc.) versus numeric values. For example, I would code this object without quotes:

x <- 5

But I wouldn't do this:

x <- cat

This is because R recognizes normal text as functions or objects, so you have to specify with a unique qualifier in order for R to read it, such as below:

x <- "cat"

However, thats not the end-all of quotes in R. Sometimes it can be quite package specific, such as ggpubr:

ggboxplot(data = df,
x = "Work_Day",
y = "Minutes"

So the short answer is, its usually to do with R understanding what you are doing with characters. Otherwise it read things as other things in the environment that aren't what you are specifying. For a good example, the example I just gave for cat is actually a function called cat, which is used for printing objects.

CodePudding user response:

When explaining these concept to absolute beginners, I like to use some layman way to explain it. It's easy to understand but not entirely accurate. But it'd be enough for beginners to remember when to use quotes and when not to use quotes.

When we use quotes "", we are telling R either it's a string, so something R don't inherently knows. For example, when we are installing new packages, we use quotes as install.packages("dplyr"), because in this case, R don't inherently knows what packages are you installing. But after we have installed the package, R knows what it is, so we can simply use library(dplyr) without quotes.

Another example would be loading in files. For example you have a CSV file that you would like to open in R, R don't inherently knows which file are you going to import, so we need quotes as in df <- read_table(file = "tell/R/your/file/location.csv"). But after you have saved your CSV in R in an object named df, we no longer need to use double quotes, because R already knows what it is.

One more example on double quotes. When we are creating stings, we need to use double quotes to tell R it's not numeric, but character. For example, when you import numbers like this phone_number <- " 1876543234", you are telling R, hey, don't treat this string of thing as number, they don't have any numeric values! Which makes sense, because you do not want R to have any arithmetic operation on phone number, otherwise it'd screw up everything.

On the other hand, when R kind of "inherently" knows the thing you are specifying, you don't need double quotes. For example as mentioned above, numeric values and logic values (TRUE or FALSE). Moreover, in function that takes a saved R object as input, you also don't need double quotes, such as ggsave(filename = "hey/R/save/the/plot/here.png", plot = p1, width = 10, limitsize = TRUE). You don't need quotes because either they are already saved as R object (p1), or they are numeric (width = 10), or they are logical (limitsize = TRUE). But you'll need quotes on filenames, since R will not know where do you want to save your output on your computer.

Hope you had a better understanding of quotes after this. But remember, this is only for beginners, you'll find tones of exception to this when you are more into R.

  • Related