Here, we show how to delete, name or rename columns and rows of data objects in R using the
subset()
, colnames()
and
rownames()
functions.
All the functions are from the 'base" package.
See also subsetting and filtering data.
Function | Usage |
subset() |
Delete or remove columns |
colnames() |
Set or reset column names |
rownames() |
Set or reset row names |
Using the dataframe below:
dtf = data.frame(Player = c("Dave", "Yiv", "Rao", "Tio"),
Group = c("A", "A", "B", "C"),
Status = c("Y", "Y", "N", "Y"),
Age = c(18, 19, 17, 19),
Score = c(15, 18, 14, 16))
dtf
Player Group Status Age Score
1 Dave A Y 18 15
2 Yiv A Y 19 18
3 Rao B N 17 14
4 Tio C Y 19 16
Delete or remove columns by name:
Player Group Status Score
1 Dave A Y 15
2 Yiv A Y 18
3 Rao B N 14
4 Tio C Y 16
Player Group Score
1 Dave A 15
2 Yiv A 18
3 Rao B 14
4 Tio C 16
Or alternatively, delete or remove columns by column numbers:
Player Group Status Score
1 Dave A Y 15
2 Yiv A Y 18
3 Rao B N 14
4 Tio C Y 16
Player Group Score
1 Dave A 15
2 Yiv A 18
3 Rao B 14
4 Tio C 16
See dtf above.
Delete some rows by value:
Player Group Status Age Score
1 Dave A Y 18 15
2 Yiv A Y 19 18
4 Tio C Y 19 16
Player Group Status Age Score
1 Dave A Y 18 15
2 Yiv A Y 19 18
Or alternatively, delete or remove rows by row numbers:
Player Group Status Age Score
1 Dave A Y 18 15
2 Yiv A Y 19 18
4 Tio C Y 19 16
Player Group Status Age Score
1 Dave A Y 18 15
2 Yiv A Y 19 18
See dtf above.
Player Group Status Age Score
1 Dave A Y 18 15
2 Yiv A Y 19 18
3 Rao B N 17 14
4 Tio C Y 19 16
Set or reset column names:
Name Class New? Age Points
1 Dave A Y 18 15
2 Yiv A Y 19 18
3 Rao B N 17 14
4 Tio C Y 19 16
Set or reset row names:
Name Class New? Age Points
Player 1 Dave A Y 18 15
Player 2 Yiv A Y 19 18
Player 3 Rao B N 17 14
Player 4 Tio C Y 19 16
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