You should use either indexing or the subset
function. For example :
R> df <- data.frame(x=1:5, y=2:6, z=3:7, u=4:8) R> df x y z u 1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7 5 5 6 7 8
Then you can use the which
function and the -
operator in column indexation :
R> df[ , -which(names(df) %in% c("z","u"))] x y 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6
Or, much simpler, use the select
argument of the subset
function : you can then use the -
operator directly on a vector of column names, and you can even omit the quotes around the names !
R> subset(df, select=-c(z,u)) x y 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6
Note that you can also select the columns you want instead of dropping the others :
R> df[ , c("x","y")] x y 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 R> subset(df, select=c(x,y)) x y 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6
Related Posts:
- Plot multiple lines in one graph
- Replace a value in a data frame based on a conditional (`if`) statement
- Create empty data frame with column names by assigning a string vector?
- Changing font size and direction of axes text in ggplot2
- How to randomize ggplot factor color scheme, keeping same default distances on color wheel?
- Warning message: line appears to contain embedded nulls [duplicate]
- Find duplicate values in R
- Error in library(ggplot2) : There is no package called ‘ggplot2’
- Transpose a data frame
- How to reshape data from long to wide format
- What’s the difference between lapply and do.call?
- Combine two or more columns in a dataframe into a new column with a new name
- Initial value in ‘vmmin’ is not finite even when changing the starting value
- Insert picture/table in R Markdown
- Test if a vector contains a given element
- How to remove outliers from a dataset
- Insert a blank column in dataframe
- “length of ‘dimnames’ [2] not equal to array extent” on one of two very similar sets
- How to install RHadoop packages (Rmr, Rhdfs, Rhbase)?
- Error in dev.off() : cannot shut down device 1 (the null device)
- How to create an empty matrix in R?
- Warning message: In `…` : invalid factor level, NA generated
- What is the difference between = and ==?
- promise already under evaluation: recursive default argument reference or earlier problems?
- What does the error “arguments imply differing number of rows: x, y” mean?
- Error in model.frame.default: variable lengths differ
- Non-numeric argument to mathematical function
- Position-dodge warning with ggplot boxplot?
- data.table vs dplyr: can one do something well the other can’t or does poorly?
- R – Concatenate two dataframes?
- KNN in R: ‘train and class have different lengths’?
- Error in rep(1, n.ahead) : invalid ‘times’ argument in R
- ggplot geom_text font size control
- lme4::lmer reports “fixed-effect model matrix is rank deficient”, do I need a fix and how to?
- R multiple conditions in if statement
- How do you use “<<-" (scoping assignment) in R?
- Remove quotes from a character vector in R
- Convert data.frame columns from factors to characters
- Understanding the result of modulo operator: %%
- Simple manual RMarkdown tables that look good in HTML, PDF and DOCX
- dplyr: “Error in n(): function should not be called directly”
- how to increase the limit for max.print in R
- In R formulas, why do I have to use the I() function on power terms, like y ~ I(x^3)
- Filter data.frame rows by a logical condition
- How to plot 3D scatter diagram using ggplot?
- Error while creating heatmaps – NA/NaN/Inf in foreign function call (arg 11)
- Write lines of text to a file in R
- How to compute summation in r
- Plot multiple boxplot in one graph
- R – argument is of length zero in if statement
- ‘Incomplete final line’ warning when trying to read a .csv file into R
- How to solve ‘protection stack overflow’ issue in R Studio
- Conditional mean statement
- Merging data – Error in fix.by(by.x, x)
- Warning in GLM analysis
- Error in 2:n : NA/NaN argument
- Error in generating boxplot two variable data frame: adding class “factor” to an invalid object
- Error : Unable to start png() device
- What does ‘length of ‘dimnames’ [1] not equal to array extent’ mean?
- How do I flip rows and columns in R
- How to catch integer(0)?
- Print string and variable contents on the same line in R
- “no function to return from, jumping to top level”
- Does R have a wildcard expression (such as an asterisk (*))?
- r random forest error – type of predictors in new data do not match
- env=baseenv() in R
- How to set axis range R
- Removing punctuations from text using R
- How to melt and cast dataframes using dplyr?
- What does Continuous x aesthetic — did you forget aes(group=…) mean?
- How to unload a package without restarting R
- What does the error “object not interpretable as a factor” mean?
- use first row data as column names in r
- Gradient of n colors ranging from color 1 and color 2
- How to prevent scientific notation in R?
- What is the difference between require() and library()?
- How to declare a vector of zeros in R
- Launching R help: Error in file(out, “wt”) : cannot open the connection
- R Sweave: NO TeX installation detected
- Plotting legend outside plot in R
- Boolean operators && and ||
- Reset par to the default values at startup
- ERROR: [on_request_read] connection reset by peer in R shiny
- Get dplyr count of distinct in a readable way
- How to counter the ‘non-numeric matrix extent’ error in R?
- What does is.na() applied to non-(list or vector) of type ‘NULL’ mean?
- error r: invalid subscript type “closure” in a simple regression
- r function unzip error 1 in extracting from zip file
- Get the list of installed packages by user in R
- Colour points in a plot differently depending on a vector of values
- Mask output of `The following objects are masked from….:` after calling attach() function
- Use of ~ (tilde) in R programming Language
- Error: x must be atomic for ‘sort.list’
- Transposing in dplyr
- Does the c command create a row vector or a column vector by default in R
- Can dplyr join on multiple columns or composite key?
- Convert data.frame column to a vector?
- Converting from a character to a numeric data frame
- How to create lag variables
- When trying to replace values, “missing values are not allowed in subscripted assignments of data frames”