How to find the object size in R?


To find the object size in R, we can use object.size function. For example, if we have a data frame called df then the size of df can be found by using the command object.size(df). Similarly, if we have a vector say x then it’s size can be found using object.size(x) and for a matrix M it can be object.size(M).

Example1

Consider the below data frame −

 Live Demo

x<-rnorm(20,31,1.2)
y<-rnorm(20,11,1.02)
df<-data.frame(x,y)
df

Output

      x         y
1  30.18391  11.927555
2  31.48452  12.532072
3  30.90274  10.478366
4  31.29327  12.735495
5  30.85212  11.633302
6  30.95957  10.577667
7  31.93196  10.688302
8  31.44202  9.357698
9  30.71209  10.485640
10 29.59055  9.758622
11 30.05166  10.119274
12 29.53356  10.570536
13 29.59849  10.419844
14 32.09375  9.422259
15 30.68645  10.763395
16 30.72715  10.302399
17 30.35040  10.029052
18 30.67613  11.189120
19 31.17024  10.881544
20 31.16995  10.292045

Finding the size of df −

object.size(df)

1168 bytes

Example2

 Live Demo

M<-matrix(rpois(40,2),ncol=2)
M

Output

      [,1] [,2]
[1,]  3     5
[2,]  1     1
[3,]  3     1
[4,]  1     2
[5,]  1     3
[6,]  4     1
[7,]  3     2
[8,]  3     5
[9,]  2     1
[10,] 0     2
[11,] 2     2
[12,] 0     1
[13,] 2     2
[14,] 3     1
[15,] 2     1
[16,] 2     3
[17,] 3     1
[18,] 4     3
[19,] 0     3
[20,] 2     0

Finding the size of M −

object.size(M)

376 bytes

Example3

 Live Demo

Vector<-rnorm(80)
Vector

Output

[1] 1.874798574 3.065711367 -0.226342501 -1.630640967 1.569689051
[6] 0.010340611 0.630591302 0.221061730 0.266599159 0.495172345
[11] 0.657045262 0.188151147 0.570511487 -0.455648139 0.943430052
[16] -0.379188331 -0.515241888 0.564965055 -0.114529886 -0.972159831
[21] -0.166275879 0.354993827 -0.389982234 0.703511422 -1.774948011
[26] -0.547321086 -0.052452074 -0.287670445 -1.212082503 -0.244187906
[31] -0.805242570 -0.852252853 0.391037408 -1.308186792 -0.822894454
[36] 0.069047146 1.662590914 0.101213549 -0.399347633 -0.148613561
[41] 0.457625125 -1.230497079 -0.121780006 -0.328781186 -0.471229168
[46] 1.511665965 0.777746188 0.231155909 -0.788583104 -1.727187548
[51] 0.098949035 -0.289089709 -0.754034076 0.411708228 -0.887566160
[56] 0.078084085 0.764479490 -0.625264125 0.638523939 -0.618117257
[61] -0.072918196 -1.281682865 0.273314251 0.485498195 2.109233979
[66] -1.066833468 -0.616190594 -1.677801977 1.101293807 0.326469848
[71] -1.074889880 -0.002953253 -0.828648472 -1.056884432 0.376533898
[76] 0.493340182 -0.564361026 1.324335702 1.020498546 -0.938115868

Finding the size of Vector −

object.size(Vector)

688 bytes

Example4

 Live Demo

List<-list(x1=rnorm(20,3,1),x2=rnorm(20,10,0.35),x3=rnorm(20,35,4.5))
List

Output

$x1
[1] 3.0269793 4.1576832 2.8207141 2.3934162 3.6544547 3.1525963 1.6441468
[8] 2.6595524 4.4163661 3.2458959 4.2298106 2.4497147 1.8276739 2.5282615
[15] 2.3205322 4.4916580 3.7016272 0.9758272 4.3550665 4.1302657
$x2
[1] 10.585657 9.468228 10.176733 9.610174 9.982476 10.537840 10.187857
[8] 10.478789 10.191026 9.362443 9.717781 9.603388 9.743209 9.748887
[15] 10.050853 10.426557 9.379878 10.021754 9.359991 10.642187
$x3
[1] 41.36769 37.02543 43.35272 34.79868 33.92261 37.14900 39.43643 33.21250
[9] 37.72320 33.59261 30.01434 35.84111 28.34595 31.75667 39.80058 38.52915
[17] 32.73363 41.52872 38.06228 32.51160

Finding the size of List −

object.size(List)

1064 bytes

Updated on: 06-Mar-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements