R Programming
  • The wikipedia of R by me
  • Hello R
    • -What is R & RStudio
    • -Learning sources
    • -R online editor
    • -R environment
  • Data types
    • -Dealing with Number
    • -Dealing with String
    • -Dealing with Dates
    • -Dealing with NA's
    • -Dealing with Logicals
    • -Dealing with Factors
  • R data
    • -R object
    • -Data structures
      • --Basics
      • --Managing Vectors
      • --Managing Matrices
      • --Managing Data Frames
    • -Functions
    • -Importing/exporting data
    • -Shape&Transform data
    • -R management
  • Visualizations
  • Intro to R Bootcamp
    • -01-introduction
    • -02-data preparation
    • -03-data transformation
    • -04-visualization
  • R programming track
    • -a-Introduction to R
      • --1-Intro to basics
      • --2-Vectors
      • --3-Matrices
      • --4-Factors
      • --5-Data frames
      • --6-Lists
    • -b-Intermediate R
      • --1-Conditionals and Control Flow
      • --2-Loops
      • --3-Functions
      • --4-The apply family
      • --5-Utilities
    • -d-Writing Functions in R
      • --1-A quick refresher
      • --2-When and how you should write a function
      • --3-Functional programming
      • --4-Advanced inputs and outputs
      • --5-Robust functions
  • Data Wrangling with R
  • R-tutor
    • #R introduction
    • #Elementary Statistics with R
  • Hands-On Programming with R
  • R for Data Science
  • Advanced R
  • ggplot2
  • R packages
  • Statistik-1
  • Statistik-2
  • Statistik-3
  • Zeitreihen & Prognosen
  • Descriptive Analytics
  • Predictive Analytics
  • Prescriptive Analytics
  • R Graphics Cookbook
    • ggplot2 intro
    • ggplot2 custome
    • ggplot top-50
  • #Exploratory Data Analysis
    • -Data Summary
    • -Checklist Solution
  • #Data Mining
    • Untitled
    • Untitled
  • #Machine Learning
    • Intro to ML
    • Intro alghorithms
    • 1. Supervised Learning
  • Master R for Data Science
    • Learning R
    • Untitled
    • Untitled
  • Data Science Projects
    • Simple linear regression:
Powered by GitBook
On this page
  1. R data

-R object

In R the variables are not declared as data type but as objects. The data type of the R-object becomes the data type of the variable.

There are many types of R-objects. The frequently used ones are Vectors, Matrices, Arrays, Data frame, List, and Factor

Vectors:

A vector is a sequence of same datatype elements

a <-c(1,2,3,4.9,10,2,4) # numeric 
vectorb <-c("one","two","three") # character 
vectorc <-c(TRUE,FALSE) #logical vectora;b;c

Get the position of the elements using vector[c(postion)]

 b[c(1,2)] # 1st and 2nd elements of vector b

Matrix:

A matrix is two-dimensional vector. The matrix elements must be of the same data type

c<- matrix( 
c(1:6),  # the data elements 
nrow=2,  # number of rows 
ncol=3,  # number of columns 
byrow = TRUE)  # fill matrix by rows  
c                    # print the matrix 
#####################     
[,1] [,2] [,3] 
[1,]    1    2    3 
[2,]    4    5    6

Array:

An array is matrix with more than 2-dimensions (n-matrix)

a<-array( 
c(1:9),  # the data elements
dim=c(3,3,2) #  2-matrix from 3 row and 3 column )
a     #print array a 
#########################
, , 1  
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9​

, , 2​     
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9​

# Get the position of the elements using array[c(postion)]
a[c(1,8)]
##########
[1] 1 8

Data frame:

A data frame is tabular store system. The column must be same length. Data frame used as the by most of R's modeling software

name<-c("cevi","Julia","Marissa")
class<-c(1,2,3)
note<-c(1,1.7,2.3)
student<-data.frame(name,class,note)
print(student)​ 
##############    
name class note
1    cevi     1  1.0
2   Julia     2  1.7
3 Marissa     3  2.3

List:

A list is a vector containing other objects. Every object can be different length and datatype

x<-c(1:10)
y<-c(TRUE,FALSE)
z<-c("cevi","Julia","Marissa")
thelist<-list(x,y,z)
print(thelist)
############
[[1]] 
[1]  1  2  3  4  5  6  7  8  9 10​
[[2]] 
[1]  TRUE, FALSE
​[[3]][1] "cevi"    "Julia"   "Marissa"​

Factor and Order

Factor create a distinct value from the data and ordered() create a ordinal structure of the data (based on alphabetical or numerical value)

gender <-c("male","male","female")
genderfactor(gender)
##############
[1] male   male   female
Levels: female male

​#ordered()
rating<-c(1,2,2,3,4,5,6,6,6,4)
ordered(rating)
################
[1] 1 2 2 3 4 5 6 6 6 4
Levels: 1 < 2 < 3 < 4 < 5 < 6
PreviousR dataNext-Data structures

Last updated 6 years ago