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.
Create a dataframe with numeric (or double) and character elements with column names from different vectors:
Team Score Position
1 A 8 3
2 B 10 3
3 B 7 1
Without column names:
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:
X1 X2 X3
1 NA NA NA
2 NA NA NA
Add or change column names of a dataframe:
Year Group Size
1 NA NA NA
2 NA NA NA
Add or change row names of a dataframe:
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).
Year Group Size
Sally 1994 NA 6
Aver 1994 5 5
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 |
Check the dimension of a dataframe (# of rows, # of columns):
Group Score Time
1 A 8 11
2 B 9 14
[1] 2 3
Check if an object is a dataframe:
Group Score Time
1 A 8 11
2 B 9 14
[1] TRUE
Check the data type or mode of a dataframe:
This should return “list” as dataframes are heterogeneous.
[1] "list"
Using the str()
function will return the data type or
mode for each column as well as the structure of the dataframe:
'data.frame': 2 obs. of 2 variables:
$ Group: chr "A" "B"
$ Score: num 8 9
Coerce an object into a dataframe:
This example turns a matrix into a dataframe, however, this is also applicable to arrays.
[,1] [,2]
[1,] 10 20
[2,] 40 80
V1 V2
1 10 20
2 40 80
The examples below add columns in a dataframe, and subtract a number from a dataframe column.
Total Score1 Score2
1 NA 8 11
2 NA 9 14
Total Score1 Score2
1 19 8 11
2 23 9 14
Total Score1 Score2
1 19 8 10
2 23 9 13
Example includes multiplying two columns and dividing by a constant. Both multiplication and division operations are similarly performed.
Total Score1 Score2
1 19 8 10
2 23 9 13
Total Score1 Score2
1 80 8 10
2 117 9 13
Total Score1 Score2
1 80 8 3.333333
2 117 9 4.333333
For character columns in a dataframe, you can use the
paste()
or paste0()
(with no spacing) to join
strings to components:
Group Year Score
1 A 1994 8
2 B 1992 9
Group Year Score
1 This is Group A Score for 1994 1994 8
2 This is Group B Score for 1992 1992 9
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes