How to Create Data Frame in R

   Data Frame is widely used and very helpful object container to process data in R. It is a two-dimensional array table that contains columns and rows. Data frame can contain character, numeric, and factor type data. It can be easily created and applied to hold data in R.
   In this post, we'll learn briefly how to create and use data frame in R.

Creating a data frame

ids=seq(1:10)
chars=sample(LETTERS, 10)
scores=runif(10, 0, 5)
df=data.frame(id=ids, char=chars, score=scores)
df
   id char     score
1   1    K 1.9857598
2   2    L 0.9887237
3   3    N 4.1596378
4   4    Q 0.7644361
5   5    U 4.0170927
6   6    M 2.7341308
7   7    I 3.3115882
8   8    Z 0.8584925
9   9    B 3.1652768
10 10    E 1.5593487

class(df)
[1] "data.frame"

Or we can create it in one line.

df=data.frame(
  id=seq(1:10), 
  char=sample(LETTERS, 10), 
  score=runif(10, 0, 5))
df
   id char       score
1   1    J 3.550912007
2   2    G 0.003123867
3   3    T 2.376582870
4   4    K 1.100594426
5   5    R 1.899082689
6   6    V 3.063855016
7   7    P 1.758989546
8   8    I 0.555677122
9   9    N 1.218097364
10 10    W 3.340277937

Here, we've created a simple R data frame with 3 columns and 10 rows.

Some simple command with data frame data

nrow(df)   # row number
[1] 10
 
ncol(df)   # column number
[1] 3
 
colnames(df)   # columns names 
[1] "id"    "char"  "score"
 
df$score     #  shows score column data 
 [1] 1.9857598 0.9887237 4.1596378 0.7644361 4.0170927 2.7341308 
     3.3115882 0.8584925 3.1652768 1.5593487
 
df[,1]        # shows 1 column items
 [1]  1  2  3  4  5  6  7  8  9 10
 
df[,2]
 [1] K L N Q U M I Z B E
Levels: B E I K L M N Q U Z
 
df[1,]          # shows 1 row items
  id char   score
1  1    K 1.98576
 
df[2,]
  id char     score
2  2    L 0.9887237

Save and load data frame data

save(df, file="df.RData")
 
df=NULL
df
NULL 
 
load("df.RData")
df
   id char    score
1   1    T 2.674877
2   2    W 2.506122
3   3    F 1.488247
4   4    D 3.397187
5   5    J 4.778997
6   6    R 4.288381
7   7    B 1.435225
8   8    A 1.836711
9   9    C 4.738932
10 10    G 2.183153

A 'rbind' and 'cbind' operation

   We can start data frame with NULL values, collect column and row data, and convert it into a data frame type.

mark=NULL
mark=cbind(mark, id=sample(1:10,10))
mark=cbind(mark, alpha=sample(letters,10))
class(mark)
[1] "matrix"
 
mark=data.frame(mark)
class(mark)
[1] "data.frame"
mark
   id alpha
1   9     s
2   4     i
3   3     j
4   2     z
5   7     u
6   8     f
7   6     t
8   1     n
9  10     m
10  5     a

Next, we'll create an empty data frame and bind rows with rbind command.

mark = data.frame(id=c(),name=c(),score=c());
mark
data frame with 0 columns and 0 rows
 
for(i in 1:10)
{
  row=data.frame("id"=i, "name"=LETTERS[i], "score"=10/i*2)
  mark=rbind(mark, row)
}
mark
   id name     score
1   1    A 20.000000
2   2    B 10.000000
3   3    C  6.666667
4   4    D  5.000000
5   5    E  4.000000
6   6    F  3.333333
7   7    G  2.857143
8   8    H  2.500000
9   9    I  2.222222
10 10    J  2.000000

In this post, we've briefly learned how to create and use data frames in R.

No comments:

Post a Comment