Here, we will discuss dataframes in R, how to create dataframes, coerce dataframes, and operations like add, subtract, multiply and divide dataframes.

A dataframe contains heterogeneous data types, hence, its composition can be a combination of the numeric (or double), integer, character, logical, complex or raw data types in different columns. It has 1 or 2 dimensions, the rows (also the first dimension) are horizontal while the columns (also the second dimension) are vertical.

1 Create Dataframes in R

Create a dataframe with numeric (or double) and character elements with column names from different vectors:

dtfrm1 = data.frame(Team = c("A", "B", "B"), 
                   Score = c(8, 10, 7), 
                   Position = c(3, 3, 1))
dtfrm1
  Team Score Position
1    A     8        3
2    B    10        3
3    B     7        1

Without column names:

dtfrm2 = data.frame(c("A", "B", "B"),
                    c(8, 10, 7),
                    c(3, 3, 1))
dtfrm2
  c..A....B....B.. c.8..10..7. c.3..3..1.
1                A           8          3
2                B          10          3
3                B           7          1

Create an empty dataframe from an empty matrix:

dtfrm3 = data.frame(matrix(NA, nrow = 2, ncol = 3))
dtfrm3
  X1 X2 X3
1 NA NA NA
2 NA NA NA

Add or change column names of a dataframe:

colnames(dtfrm3) = c("Year", "Group", "Size")
dtfrm3
  Year Group Size
1   NA    NA   NA
2   NA    NA   NA

Add or change row names of a dataframe:

rownames(dtfrm3) = c("Sally", "Aver")
dtfrm3
      Year Group Size
Sally   NA    NA   NA
Aver    NA    NA   NA

Assign values to (or edit) some index of a dataframe:

First number (or set of numbers) is for the rows (horizontal), while the second number (or set of numbers) is for the columns (vertical).

dtfrm3[,1] = 1994
dtfrm3[1,3] = 6
dtfrm3[2,2:3] = 5
dtfrm3
      Year Group Size
Sally 1994    NA    6
Aver  1994     5    5
Summary of the Functions to Coerce or Check Dataframes in R
Function Usage
dim() Check or set dimension
is.data.frame() Check if dataframe
typeof() Check data type
str() Check data type of columns
as.data.frame() Coerce into dataframe
  • See examples below.

2 Check Dataframes in R

Check the dimension of a dataframe (# of rows, # of columns):

dtfrm1 = data.frame(Group = c("A", "B"),
                    Score = c(8, 9),
                    Time = c(11, 14))
dtfrm1
  Group Score Time
1     A     8   11
2     B     9   14
dim(dtfrm1)
[1] 2 3

Check if an object is a dataframe:

dtfrm1 = data.frame(Group = c("A", "B"),
                    Score = c(8, 9),
                    Time = c(11, 14))
dtfrm1
  Group Score Time
1     A     8   11
2     B     9   14
is.data.frame(dtfrm1) # This will return TRUE or FALSE.
[1] TRUE

Check the data type or mode of a dataframe:

This should return “list” as dataframes are heterogeneous.

dtfrm1 = data.frame(Group = c("A", "B"),
                    Score = c(8, 9))
typeof(dtfrm1)
[1] "list"

Using the str() function will return the data type or mode for each column as well as the structure of the dataframe:

str(dtfrm1)
'data.frame':   2 obs. of  2 variables:
 $ Group: chr  "A" "B"
 $ Score: num  8 9

3 Coerce into Dataframes in R

Coerce an object into a dataframe:

This example turns a matrix into a dataframe, however, this is also applicable to arrays.

mat1 = matrix(c(10, 20, 40, 80),
              nrow = 2, byrow = TRUE)
mat1
     [,1] [,2]
[1,]   10   20
[2,]   40   80
dtfrm1 = as.data.frame(mat1)
dtfrm1
  V1 V2
1 10 20
2 40 80

4 Add and Subtract Columns in Dataframes in R

The examples below add columns in a dataframe, and subtract a number from a dataframe column.

dtfrm1 = data.frame(Total = rep(NA, times = 2),
                    Score1 = c(8, 9),
                    Score2 = c(11, 14))
dtfrm1
  Total Score1 Score2
1    NA      8     11
2    NA      9     14
# Add columns in a dataframe
dtfrm1[,1] = dtfrm1[,2] + dtfrm1[,3]
dtfrm1
  Total Score1 Score2
1    19      8     11
2    23      9     14
# Subtract a number from a dataframe column
dtfrm1[,3] = dtfrm1[,3] - 1
dtfrm1
  Total Score1 Score2
1    19      8     10
2    23      9     13

5 Multiply and Divide Columns in Dataframes in R

Example includes multiplying two columns and dividing by a constant. Both multiplication and division operations are similarly performed.

dtfrm1
  Total Score1 Score2
1    19      8     10
2    23      9     13
# Multipy two columns from a dataframe
dtfrm1[,1] = dtfrm1[,2] * dtfrm1[,3]
dtfrm1
  Total Score1 Score2
1    80      8     10
2   117      9     13
# Divide a column by a number
dtfrm1[,3] = dtfrm1[,3] / 3
dtfrm1
  Total Score1   Score2
1    80      8 3.333333
2   117      9 4.333333

6 Join Strings from Character Dataframes in R

For character columns in a dataframe, you can use the paste() or paste0() (with no spacing) to join strings to components:

dtfrm1 = data.frame(Group = c("A", "B"), 
                    Year = c("1994", "1992"),
                    Score = c(8, 9))
dtfrm1
  Group Year Score
1     A 1994     8
2     B 1992     9
dtfrm1[,1] = paste("This is Group", dtfrm1[,1], "Score for", dtfrm1[,2])
dtfrm1
                           Group Year Score
1 This is Group A Score for 1994 1994     8
2 This is Group B Score for 1992 1992     9

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes