R in Clinical World: WEEK4

PinnacleVex KA Analytics
3 min readMay 21, 2021

--

R Data types
1. Vectors(Basic R Type)
2. Data Frames(Collection of vectors)
3. Lists(Collection of R objects)
4. Other type:
Matrix
Factor
Array

1. Vectors: The basic data structure in R is the vector (Default), and are the simplest R objects (an ordered list of objects of a given type).
Vectors are indexed by integers starting at 1.
We can create a vector using the c() function which concatenates some elements.

Vector Types: A. Numeric and B. Character Vectors
class(company)
class(revenue)

company<-”PinnacleVex KA Analytics”
is.vector(company)
revenue<-100000
is.vector(revenue)

student_name<- c(“Ape”, “Bat”, “Camel”, “Dog”, “Elephant”)
age_student <- c(14, 15, 16, 14, 15)
english_marks<- c(70, 86, 90, 98, 85)
science_marks<- c(95, 90, 64, 89, 88)

is.vector(age_student)
is.vector(english_marks)
is.vector(student_name)

We can apply most of the mathematical functions, and operators to vectors without writing any loops.
age_student+3
english_marks2<- english_marks+5
Total<- english_marks2 + science_marks
Total
age_student/Total

Accessing Vector Elements:
[] => We use the [] operator to select elements
: => Used to select or exclude specific elements :
- => Used to exclude negate index, or vector of indexes

age_student
age_student[2]
age_student[2:5]
age_student[-3] #It write all elements except 3 (excluded 3rd element)
age_student[2]<-18 #Updating 2nd element
age_student[4]<-19 #Updating 4th element
age_student

We have 5 elements in this vector, if we want to add another element then simply we can add

age_student[6]<-22
age_student

2. R Data frames: These are collection of related vectors.
Data frames are similar to dataset or tables.
In general, when data is imported from external sources, it will be stored as a data frame.
To create a data frame, we need to use data.frame function.
DataFrames are heterogeneous(contains multiple data types in multiple column).

student_name<- c(“Ape”, “Bat”, “Camel”, “Dog”, “Elephant”)
age_student <- c(14, 15, 16, 14, 15)
english_marks<- c(70, 86, 90, 98, 85)
science_marks<- c(95, 90, 64, 89, 88)

Here we are create a data frame using 4 individual vectors(Mostly we won’t create data frame, as we import from external source).

students_data <-data.frame(student_name, age_student, english_marks, science_marks)
students_data

students_data2<-c(student_name, age_student, english_marks, science_marks)
students_data2
str(students_data)
str(students_data2)

Accessing a column or row or an element from the data frame:
We can use dollar($) followed by variable name or dataframe with [] or matrix indexes.

Method1:

students_data$student_name
students_data$age_student
students_data$english_marks

Method2:

students_data[“student_name”]
students_data[“age_student”]

Method3: Matrix index format [rows, columns]

students_data[1,] #1 rows and all columns
students_data[,1] #all rows and 1 column
students_data[,3:5] #all rows and 3,4,5 columns
students_data[1:5, ] #5 rows and all columns
students_data[,-2] #all rows and exept 1st column
students_data[-1,] #all rows except 2st row and all columns
students_data[,c(1,2)] #all rows and 1, 2 columns
students_data[c(1,5),c(1,2)] #rows 1 and 5, and columns 1 and 2
students_data[c(1:5),c(1,2)] #rows 1 to 5, and columns 1 to 5

Note: These three methods will not give same type of results, so check data type using str().
a <- students_data$student_name
b <- students_data[“student_name”]
c <- students_data[,1]
str(a) # gives an object/factor
str(b) #data frame
str(c) # an object/factor

Practice: Load Built-in datasets
library(datasets)
data()
ToothGrowth
iris
AirPassengers
mtcars

head(iris)
tail(iris)
summary(iris)

help(iris) or ?iris

Happy Learning !!! :)

Related Articles:

R in Clinical World: WEEK3

R in Clinical World: WEEK2

R in Clinical World: WEEK1

Introduction to POWER BI

PinnacleVex KA Analytics: BASICS OF MICROSOFT EXCEL

PinnacleVex KA Analytics: Basic Excel Formulas For Your Daily Workflow

PinnacleVex KA Analytics: Simple introduction to SQL

PinnacleVex KA Analytics: Clinical Trials

PinnacleVex KA Analytics Clinical Data Management

My_website

LinkedIn

Youtube

Facebook

Twitter

Instagram

Youtube

--

--

PinnacleVex KA Analytics
PinnacleVex KA Analytics

Written by PinnacleVex KA Analytics

PinnacleVex KA Analytics is a Healthcare and IT Service Provider Headquartered at Hyderabad, India.

No responses yet