R Programming Manual for Visual Studio Code
Setting Up R in VSCode
Prerequisites
- Install R
- Download from CRAN
- Install R base system on your operating system
- Verify installation by opening terminal and typing
R --version
- Install Rtools (Windows Only)
- Download from CRAN Rtools
- Required for building packages from source
VSCode Configuration
- Install R Extension
- Open VSCode Extensions panel (
Ctrl+Shift+X
)
- Search for "R" by REditorSupport
- Install the extension
- Configure R Path
- Open Settings (
Ctrl+,
)
- Search for "r.rterm"
- Set the path to your R executable:
- Windows:
C:\Program Files\R\R-4.x.x\bin\x64\R.exe
- Mac:
/usr/local/bin/R
- Linux:
/usr/bin/R
Essential VSCode Extensions for R
Core Extensions
- R (REditorSupport.r) - Syntax highlighting, IntelliSense, and debugging
- R LSP Client (REditorSupport.r-lsp) - Language server protocol support
- R Debugger (RDebugger.r-debugger) - Advanced debugging capabilities
Recommended Extensions
- Rainbow CSV - Better CSV file viewing
- Bracket Pair Colorizer - Visual bracket matching
- GitLens - Enhanced Git integration
- Markdown All in One - For R Markdown support
R Basics and Syntax
Basic Operations
x <- 5
y = 10
15 -> z
a <- 10 + 5
b <- 10 - 5
c <- 10 * 5
d <- 10 / 5
e <- 10^2
f <- 10 %% 3
g <- 10 %/% 3
10 == 10
10 != 5
10 > 5
10 < 5
10 >= 10
10 <= 5
TRUE & FALSE
TRUE | FALSE
!TRUE
Comments and Documentation
add_numbers <- function(x, y) {
return(x + y)
}
Data Types and Structures
Basic Data Types
num <- 42.5
class(num)
int <- 42L
class(int)
char <- "Hello, World!"
class(char)
logical <- TRUE
class(logical)
complex <- 3 + 2i
class(complex)
is.numeric(num)
is.character(char)
is.logical(logical)
Data Structures
Vectors
numeric_vector <- c(1, 2, 3, 4, 5)
character_vector <- c("apple", "banana", "cherry")
logical_vector <- c(TRUE, FALSE, TRUE)
length(numeric_vector)
numeric_vector[1]
numeric_vector[c(1, 3, 5)]
numeric_vector[numeric_vector > 3]
named_vector <- c(a = 1, b = 2, c = 3)
named_vector["a"]
Lists
my_list <- list(
numbers = c(1, 2, 3),
text = "Hello",
logical = TRUE,
nested_list = list(a = 1, b = 2)
)
my_list$numbers
my_list[["numbers"]]
my_list[[1]]
my_list["numbers"]
Data Frames
df <- data.frame(
name = c("Alice", "Bob", "Charlie"),
age = c(25, 30, 35),
city = c("New York", "London", "Tokyo"),
stringsAsFactors = FALSE
)
df$name
df[["name"]]
df[1, ]
df[, 1]
df[1:2, c("name", "age")]
nrow(df)
ncol(df)
dim(df)
names(df)
str(df)
summary(df)
Matrices
matrix1 <- matrix(1:12, nrow = 3, ncol = 4)
matrix2 <- matrix(1:12, nrow = 3, ncol = 4, byrow = TRUE)
dim(matrix1)
matrix1[2, 3]
matrix1[, 1]
matrix1[1, ]
matrix1 + matrix2
matrix1 * matrix2
matrix1 %*% t(matrix2)
Working with Data
Data Import and Export
data <- read.csv("file.csv")
data <- read.csv("file.csv", header = TRUE, sep = ",")
data <- read.table("file.txt", header = TRUE, sep = "\t")
data <- read.delim("file.txt")
write.csv(data, "output.csv", row.names = FALSE)
write.table(data, "output.txt", sep = "\t", row.names = FALSE)
url_data <- read.csv("https://example.com/data.csv")
Data Manipulation
head(df)
tail(df)
View(df)
subset(df, age > 25)
df[df$age > 25, ]
df[order(df$age), ]
df[order(-df$age), ]
df$salary <- c(50000, 60000, 70000)
df$bonus <- df$salary * 0.1
df$bonus <- NULL
df <- df[, !names(df) %in% c("bonus")]
Missing Data
data_with_na <- c(1, 2, NA, 4, 5)
is.na(data_with_na)
any(is.na(data_with_na))
sum(is.na(data_with_na))
na.omit(data_with_na)
data_with_na[!is.na(data_with_na)]
mean(data_with_na, na.rm = TRUE)
Control Structures
Conditional Statements
x <- 10
if (x > 5) {
print("x is greater than 5")
} else if (x == 5) {
print("x equals 5")
} else {
print("x is less than 5")
}
numbers <- c(1, 5, 10, 15, 20)
result <- ifelse(numbers > 10, "High", "Low")
print(result)
grade <- "A"
description <- switch(grade,
A = "Excellent",
B = "Good",
C = "Average",
D = "Below Average",
F = "Fail",
"Unknown Grade"
)
Loops
for (i in 1:5) {
print(paste("Iteration:", i))
}
fruits <- c("apple", "banana", "cherry")
for (fruit in fruits) {
print(paste("I like", fruit))
}
counter <- 1
while (counter <= 5) {
print(paste("Counter:", counter))
counter <- counter + 1
}
counter <- 1
repeat {
print(paste("Counter:", counter))
counter <- counter + 1
if (counter > 5) {
break
}
}
for (i in 1:10) {
if (i %% 2 == 0) {
next
}
print(i)
}
Functions
Creating Functions
greet <- function(name) {
return(paste("Hello,", name))
}
calculate_bmi <- function(weight, height) {
bmi <- weight / (height^2)
return(bmi)
}
greet_with_title <- function(name, title = "Mr.") {
return(paste("Hello,", title, name))
}
calculate_stats <- function(numbers) {
result <- list(
mean = mean(numbers),
median = median(numbers),
sd = sd(numbers),
min = min(numbers),
max = max(numbers)
)
return(result)
}
greet("Alice")
calculate_bmi(70, 1.75)
greet_with_title("Smith", "Dr.")
stats <- calculate_stats(c(1, 2, 3, 4, 5))
Built-in Functions
abs(-5)
sqrt(16)
log(10)
log10(100)
exp(1)
round(3.14159, 2)
ceiling(3.2)
floor(3.8)
nchar("Hello")
toupper("hello")
tolower("HELLO")
substr("Hello World", 1, 5)
paste("Hello", "World")
paste0("Hello", "World")
mean(c(1, 2, 3, 4, 5))
median(c(1, 2, 3, 4, 5))
sd(c(1, 2, 3, 4, 5))
var(c(1, 2, 3, 4, 5))
min(c(1, 2, 3, 4, 5))
max(c(1, 2, 3, 4, 5))
range(c(1, 2, 3, 4, 5))
Data Visualization
Base R Graphics
x <- 1:10
y <- x^2
plot(x, y, main = "Scatter Plot", xlab = "X values"