Graphics Reference
In-Depth Information
[ 1 ] "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC"
[ 16 ] "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC" "VC"
[ 31 ] "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ"
[ 46 ] "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ" "OJ"
Adding a Column to a Data Frame
Problem
You want to add a column to a data frame.
Solution
Just assign some value to the new column.
If you assign a single value to the new column, the entire column will be filled with that value.
This adds a column named newcol , filled with NA :
data$newcol <- NNA
You can also assign a vector to the new column:
data$newcol <- vec
If the length of the vector is less than the number of rows in the data frame, then the vector is
repeated to fill all the rows.
Discussion
Each “column” of a data frame is a vector or factor. R handles them slightly differently from
standalone vectors, because all the columns in a data frame have the same length.
Deleting a Column from a Data Frame
Problem
You want to delete a column from a data frame.
Solution
Assign NULL to that column:
data$badcol <- NULL
NULL
Search WWH ::




Custom Search