Graphics Reference
In-Depth Information
Discussion
You can also use the
subset()
function and put a
-
(minus sign) in front of the column(s) to
drop:
# Return data without badcol
data
<-
subset(data, select
= -
badcol)
# Exclude badcol and othercol
data
<-
subset(data, select
=
c(
-
badcol,
-
othercol))
See Also
Getting a Subset of a Data Frame
for more on getting a subset of a data frame.
Renaming Columns in a Data Frame
Problem
You want to rename the columns in a data frame.
Solution
Use the
names(dat) <-
function:
names(dat)
<-
c(
"name1"
,
"name2"
,
"name3"
)
Discussion
If you want to rename the columns by name:
library(gcookbook)
# For the data set
names(anthoming)
# Print the names of the columns
"angle" "expt" "ctrl"
names(anthoming)[names(anthoming)
==
"ctrl"
]
<-
c(
"Control"
)
names(anthoming)[names(anthoming)
==
"expt"
]
<-
c(
"Experimental"
)
names(anthoming)
"angle" "Experimental" "Control"
They can also be renamed by numeric position:
names(anthoming)[
1
]
<-
"Angle"
names(anthoming)