x<-1 #expressions
print(x)## next expression [1] 1
y<-2
x+y ## add the values of x and y [1] 3
# [1] 3- []-indicate the what element of bracket is shown
SHAKTI<-x+y
z <- SHAKTI
z[1] 3
CEU, AIIMS Bhopal
November 1, 2025
Welcome to R Programming Nuts and Bolts — a foundational tutorial designed for beginners and classroom teaching.
This guide covers the basics of R programming in a structured, example-driven format with visible outputs for immediate understanding.
title: “r-nut_bolt” author: “cfm” date: ‘2022-10-06’ output: html_document: df_print: paged
knitr::opts_chunk$set(echo = TRUE)
[1] "welcome to workshop"
[1] "character"
[1] "character"
[1] 1
[1] "numeric"
[1] 2
[1] "numeric"
## There is a explicit way to mention integer using capital L suffix
2L+3L->p
## integer- a whole number with a fraction
class(p)[1] "integer"
[1] 5.8
[1] "numeric"
[1] FALSE FALSE TRUE FALSE FALSE TRUE
[1] "logical"
[1] 6
[1] FALSE FALSE TRUE TRUE TRUE FALSE
[1] "logical"
## complex -A complex value in R is defined via the pure imaginary value i.
z =1+ 2i # create a complex number
z # print the value of z [1] 1+2i
[1] "complex"
[1] "character"
[1] "integer"
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[49] FALSE
[1] "logical"
### what if I mix different atomic class of object
##Implicit coercion- R will create a resulting vector with a mode that can most easily accommodate all the elements it contains. This conversion between modes of storage is called “coercion”. When R converts the mode of storage based on its content, it is referred to as “implicit coercion”. (every object of the vector becomes same class.)
o4<-c(v1,v2,v3)
class(o4)[1] "character"
chr [1:102] "ram" "shyam" "rahul" "radha" "1" "2" "3" "4" "5" "6" "7" "8" ...
##create a vector and you mix two different types of objects
o5<-c(1,2,3,"ram","shyam")
sir<-c(1,2,3,8.9,9.23)
o6<-c(o5,sir)
length(o6)[1] 10
[1] "3"
[1] "character"
[1] TRUE TRUE TRUE FALSE FALSE
[1] "logical"
List of 5
$ : chr [1:4] "ram" "shyam" "rahul" "radha"
$ : int [1:49] 1 2 3 4 5 6 7 8 9 10 ...
$ : logi [1:49] TRUE TRUE TRUE TRUE TRUE TRUE ...
$ : chr [1:102] "ram" "shyam" "rahul" "radha" ...
$ : chr [1:5] "1" "2" "3" "ram" ...
[[1]]
[1] "ram" "shyam" "rahul" "radha"
[[2]]
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
[[3]]
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[49] FALSE
[[4]]
[1] "ram" "shyam" "rahul" "radha" "1" "2" "3" "4" "5"
[10] "6" "7" "8" "9" "10" "11" "12" "13" "14"
[19] "15" "16" "17" "18" "19" "20" "21" "22" "23"
[28] "24" "25" "26" "27" "28" "29" "30" "31" "32"
[37] "33" "34" "35" "36" "37" "38" "39" "40" "41"
[46] "42" "43" "44" "45" "46" "47" "48" "49" "TRUE"
[55] "TRUE" "TRUE" "TRUE" "TRUE" "TRUE" "TRUE" "TRUE" "TRUE" "FALSE"
[64] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[73] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[82] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[91] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[100] "FALSE" "FALSE" "FALSE"
[[5]]
[1] "1" "2" "3" "ram" "shyam"
[1] "ram" "shyam" "rahul" "radha"
[[1]]
[1] "ram" "shyam" "rahul" "radha" "1" "2" "3" "4" "5"
[10] "6" "7" "8" "9" "10" "11" "12" "13" "14"
[19] "15" "16" "17" "18" "19" "20" "21" "22" "23"
[28] "24" "25" "26" "27" "28" "29" "30" "31" "32"
[37] "33" "34" "35" "36" "37" "38" "39" "40" "41"
[46] "42" "43" "44" "45" "46" "47" "48" "49" "TRUE"
[55] "TRUE" "TRUE" "TRUE" "TRUE" "TRUE" "TRUE" "TRUE" "TRUE" "FALSE"
[64] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[73] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[82] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[91] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[100] "FALSE" "FALSE" "FALSE"
[[1]]
[1] "ram" "shyam" "rahul" "radha"
[1] "list"
List of 1
$ : chr [1:4] "ram" "shyam" "rahul" "radha"
[1] 1
[1] "ram" "shyam" "rahul" "radha"
[1] "character"
chr [1:4] "ram" "shyam" "rahul" "radha"
[1] 4
List of 3
$ v1: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ v2: logi TRUE
$ v3: chr [1:3] "a" "b" "c"
$v1
[1] 1 2 3 4 5 6 7 8 9 10
[1] 1 2 3 4 5 6 7 8 9 10
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[1] 2 3
[1] 6
[,1] [,2] [,3]
[1,] 2 6 10
[2,] 4 8 12
[,1] [,2] [,3]
[1,] 3 5 7
[2,] 4 6 8
$dim
[1] 2 3
[1] "0" "1"
$levels
[1] "0" "1"
$class
[1] "factor"
f2<-c("mild","mild","moderate","moderate","severe","mild","moderate")
f2<-as.factor(f2)
attributes(f2)$levels
[1] "mild" "moderate" "severe"
$class
[1] "factor"
[1] 1 1 2 2 3 1 2
attr(,"levels")
[1] "mild" "moderate" "severe"
[1] 2 3 2 2 NA 2
#Warning message:In Ops.factor(f1, f2) : ‘+’ not meaningful for factors
#Q =What is the difference between NA an NaN
# NA=Not Available ( can be with character ,numeric or logical )
# NaN=Not a Number (only with numeric vector )
### NaN is also NA but converse is not true
f3<- c(1,2,3,NA,4,5,NA)
is.na(f3)[1] FALSE FALSE FALSE TRUE FALSE FALSE TRUE
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[1] FALSE FALSE TRUE FALSE FALSE TRUE TRUE
[1] FALSE FALSE TRUE FALSE FALSE FALSE TRUE
#NaN in R means “Not a Number” which means there is something or some value, but it cannot be described in the computer. NaN designates a result that cannot be calculated for whatever reason, or it is not a floating-point number.
#NULL : is for empty object.
a<-c()
length(a)[1] 0
NULL
name<-c("A","B","C","D","H")
age<-c(32,36,29,25,21)
sex<-c("M","M","F","F","M")
hba1c<-c(6.4,7.8,9.1,6.4,10)
df<-data.frame(name,age,sex,hba1c)
attributes(df)$names
[1] "name" "age" "sex" "hba1c"
$class
[1] "data.frame"
$row.names
[1] 1 2 3 4 5
[1] 5 4
[1] "data.frame"
name age sex hba1c
1 A 32 M 6.4
2 B 36 M 7.8
3 C 29 F 9.1
4 D 25 F 6.4
5 H 21 M 10.0
name age sex hba1c
2 B 36 M 7.8
3 C 29 F 9.1
5 H 21 M 10.0
name age sex hba1c
2 B 36 M 7.8
3 C 29 F 9.1
5 H 21 M 10.0
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
name age sex hba1c
1 B 36 M 7.8
2 C 29 F 9.1
3 H 21 M 10.0
name age sex hba1c
2 B 36 M 7.8
3 C 29 F 9.1
5 H 21 M 10.0
## but if i want to extract the age only of participants having hba1c>6.5
df$age[which(df$hba1c>6.5)][1] 36 29 21
[1] "M" "F" "M"
name age sex hba1c
5 H 21 M 10
[1] "M" "M" "F" "F" "M"
[1] 10
m1<-c(3,4,6,NA,7,4)
m2<-c("a","b","c", NA,"e","f")
m3<-c(2.6,2.9,5.6,NA,NA,4.9)
df.m<-data.frame(m1,m2,m3)
str(df.m)'data.frame': 6 obs. of 3 variables:
$ m1: num 3 4 6 NA 7 4
$ m2: chr "a" "b" "c" NA ...
$ m3: num 2.6 2.9 5.6 NA NA 4.9
m1 m2 m3
Min. :3.0 Length:6 Min. :2.600
1st Qu.:4.0 Class :character 1st Qu.:2.825
Median :4.0 Mode :character Median :3.900
Mean :4.8 Mean :4.000
3rd Qu.:6.0 3rd Qu.:5.075
Max. :7.0 Max. :5.600
NA's :1 NA's :2
m1 m2 m3
1 3 a 2.6
2 4 b 2.9
3 6 c 5.6
4 NA <NA> NA
5 7 e NA
6 4 f 4.9
m1 m2 m3
1 3 a 2.6
2 4 b 2.9
3 6 c 5.6
6 4 f 4.9
Vectors are the most basic R data structures — they store elements of the same type.
# Creating vectors
num_vec <- c(1, 2, 3, 4, 5)
char_vec <- c("apple", "banana", "cherry")
num_vec
# [1] 1 2 3 4 5
char_vec
# [1] "apple" "banana" "cherry"Vector operations:
Data frames are two-dimensional tables that can hold different data types in each column.
Functions are reusable blocks of code that perform a specific task.
# Defining a function
add_numbers <- function(a, b) {
sum <- a + b
return(sum)
}
# Calling the function
add_numbers(5, 3)
# [1] 8Built-in functions:
R provides in-built documentation for almost every function.
You can also search more generally using:
Or browse online documentation:
R includes standard control structures: if, else, for, and while.
| Concept | Function | Example | Description |
|---|---|---|---|
| Assign value | <- |
x <- 5 |
Assigns 5 to x |
| Combine elements | c() |
c(1,2,3) |
Creates vector |
| Create sequence | : |
1:5 |
Generates 1,2,3,4,5 |
| Mean | mean() |
mean(c(2,4,6)) |
Computes average |
| Conditional | if() |
if(x>0) print("yes") |
Executes code if condition true |
| Data frame | data.frame() |
data.frame(Name, Age) |
Creates table-like structure |
This comprehensive document now includes your original explanations, followed by enhanced topics like Vectors, Data Frames, Functions, Help System, and Control Structures — all complete with visible outputs and syntax highlighting.
It’s ready to be used as a pedagogical classroom resource for R basics.