Data Frames in R programming

A data frame is a matrix with rows and columns populated with data. Data frame might consists of heterogeneous data. Each column might consists of different data types.

Data Frame

To begin, we will create a data frame with sample data frame example as shown below.

#R data frame example

myData <- data.frame("sno"=1:6, "weight"=76:81, "height"=171:176)

print(myData)

Output:

  sno weight height
1   1     76    171
2   2     77    172
3   3     78    173
4   4     79    174
5   5     80    175
6   6     81    176

 

 

Access data

Now that we have a data frame, let’s access the data . We can access the data in many ways such as using index values or names.

Let’s access 3rd row and second column in the data frame. We can use different commands as shown below:

myData[3,2]

myData$weight[3]

myData[3,”weight”]

myData <- data.frame("sno"=1:6, "weight"=76:81, "height"=171:176)

print(myData[3,2])

Output:

[1] 78

 

Sub setting data frame

We can use the subset() function to subset a data frame. It is R function that allows us to subset a data frame.

Let’s create a new data frame that only contained rows with weight < 79.

newData <- subset(myData, weight < 79)

 

myData <- data.frame("sno"=1:6, "weight"=76:81, "height"=171:176)

newData <- subset(myData, weight < 79)

print(newData)

Output:

  sno weight height
1   1     76    171
2   2     77    172
3   3     78    173

In case you need any help with a function, you can use the help function.

help(subset).

or

?subset

Output:

_S_u_b_s_e_t_t_i_n_g _V_e_c_t_o_r_s, _M_a_t_r_i_c_e_s _a_n_d _D_a_t_a _F_r_a_m_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     Return subsets of vectors, matrices or data frames which meet
     conditions.

_U_s_a_g_e:

     subset(x, ...)
     
     ## Default S3 method:
     subset(x, subset, ...)
     
     ## S3 method for class 'matrix'
     subset(x, subset, select, drop = FALSE, ...)
     
     ## S3 method for class 'data.frame'
     subset(x, subset, select, drop = FALSE, ...)

Related Posts