R Programming

Some useful functions for working with objects in R

We provide effective and economically affordable training courses for R and Python, click here for more details and course registration !

In this post, we will introduce and provide several useful and important functions in R. Most of them are fairly basic, and frequently used in data analysis with R programming.

length() – get the length of an object

#to show first three observations of a data frame 'grade'
head(grade,3)
  StudentID        Fullname Race Gender Country Age Math Physics
1         1     James Zhang    A   Male      US  23   73      70
2         2       Wilson Li    E Female      UK  26   95      76
3         3 Richard Nuan Ye    A   Male      UK  35   77      83
  Chemistry       Date
1        87 10/31/2008
2        83  3/16/2008
3        92  5/22/2008
#length of data frame 'grade'
length(grade)
[1] 10
#to create a vector
test<-grade$Age
#length of a vector
length(test)
[1] 20

dim() – to show the dimension of an object

#dimension of a data frame
dim(grade)
[1] 20 10

str() – to show structure of a data frame

str(grade)
'data.frame': 20 obs. of  10 variables:
 $ StudentID: chr  "1" "2" "3" "4" ...
 $ Fullname : chr  "James Zhang" "Wilson Li" "Richard Nuan Ye" "Mary Deng" ...
 $ Race     : chr  "A" "E" "A" "E" ...
 $ Gender   : chr  "Male" "Female" "Male" "Female" ...
 $ Country  : chr  "US" "UK" "UK" "US" ...
 $ Age      : num  23 26 35 21 19 43 37 28 19 25 ...
 $ Math     : num  73 95 77 60 77 79 87 95 73 66 ...
 $ Physics  : num  70 76 83 99 89 64 99 87 92 93 ...
 $ Chemistry: num  87 83 92 84 93 83 67 93 84 65 ...
 $ Date     : chr  "10/31/2008" "3/16/2008" "5/22/2008" "1/24/2009" ...

class() – to show class type of an object

#class of a data frame
class(grade)
[1] "data.frame"
#class of an vector 
class(test)
[1] "numeric"

mode() – to show the mode(data storage type) of an object

#mode of a data frame
mode(grade)
[1] "list"
#mode of a vector 
mode(test)
[1] "numeric"

names() – to show the names of an object

#names of columns of a data frame
names(grade)
 [1] "StudentID" "Fullname"  "Race"      "Gender"    "Country"  
 [6] "Age"       "Math"      "Physics"   "Chemistry" "Date" 

c() – Combine extra elements to a vector

test2<-c(test, 20, 20)  
test2
 [1] 23 26 35 21 19 43 37 28 19 25 42 32 27 35 21 29 36 39 24 25 20
[22] 20

cbind() – Combines data frames across columns

#to create a data frame of 3 columns
gradepart<-grade[,c(7:9)]
names(gradepart)<-c("v1","v2","v3")
#combine two data frames
test3<-cbind(grade,gradepart )
#show first 3 observations of the combined data frame
head(test3,3)
  StudentID        Fullname Race Gender Country Age Math Physics
1         1     James Zhang    A   Male      US  23   73      70
2         2       Wilson Li    E Female      UK  26   95      76
3         3 Richard Nuan Ye    A   Male      UK  35   77      83
  Chemistry       Date v1 v2 v3
1        87 10/31/2008 73 70 87
2        83  3/16/2008 95 76 83
3        92  5/22/2008 77 83 92
> 

rbind() – Combines data frames across rows

#to create a new data frame of 10 rows
test4<-grade[1:10,]
#combine two data frame along rows
test5<-rbind(grade, test4)
#show first 15 observations of the combined data frame 
head(test5,15)
     StudentID        Fullname Race Gender Country Age Math Physics
1          1     James Zhang    A   Male      US  23   73      70
2          2       Wilson Li    E Female      UK  26   95      76
3          3 Richard Nuan Ye    A   Male      UK  35   77      83
4          4       Mary Deng    E Female      US  21   60      99
5          5    Jason Wilson    A   Male      UK  19   77      89
6          6 Jennifer Hopkin    A Female      UK  43   79      64
7          7    Kari Gjendem    E Female      US  37   87      99
8          8     Wenche Dale    E Female      US  28   95      87
9          9     Jane Larsen    A Female      US  19   73      92
10        10  Steinar Hansen    A   Male      US  25   66      93
11        11    Michael Chen    A   Male      UK  42   83      90
12        12    Josef Curton    E   Male      US  32   71      63
13        13  Jennifer Jones    E   Male      US  27   79      76
14        14      Gary Grant    E Female      UK  35   90      78
15        15        Phil Yao    A   Male      UK  21   69      69
   Chemistry       Date
1         87 10/31/2008
2         83  3/16/2008
3         92  5/22/2008
4         84  1/24/2009
5         93  7/30/2009
6         83   4/5/2009
7         67 11/24/2008
8         93  10/2/2008
9         84   6/5/2009
10        65   8/1/2008
11        77 10/24/2008
12        96  11/8/2009
13        82 10/29/2008
14        92 10/24/2008
15        83 10/15/2008

seq() – to create a sequence of numbers into a vector

#generate a vector from 1 to 10
seq(1,10)     
 [1]  1  2  3  4  5  6  7  8  9 10
#generate a vector of 5 numbers between 1 and 10, gap 2
seq(1,10,2) 
[1] 1 3 5 7 9

rep() – generate a sequence of number with repetition

#generate a vector 1,1,1,1,1
rep(1,5)    
[1] 1 1 1 1 1
#generate a vector 1,2,1,2,1,2,1,2,1,2 
rep(c(1,2),5)
 [1] 1 2 1 2 1 2 1 2 1 2

You can also watch R tutorial videos on our YouTube channel.

wilsonzhang746

Recent Posts

Main framework for PyTorch music generation model

If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…

1 day ago

affine_grid() in pytorch

According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…

2 months ago

How to create an Android mobile app with a deep learning AI model ?

Creating an Android mobile app with a deep learning AI model involves several key steps:…

11 months ago

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

1 year ago

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

1 year ago

Install PyTorch on Windows

PyTorch is a deep learning package for machine learning, or deep learning in particular for…

1 year ago