Hi there,
I'm trying to remove multiple columns by name from a data.frame. As a
result I need to get back the modified data.frame without the removed
columns. My columns I want to delete are listed in a vector called
"delete".
data <- read.csv2("data.csv")
delete <- c("col1", "col2", "col3")
newData <- subset(data, select = delete)
newData <- data[delete]
If I try one of the above shown solutions, it only gives me back the
columns I want to delete. But I need the exact opposite.
newData <- subset(data, select = -delete)
newData <- data[-delete]
But as I try the way with a minus I'll get the message: "invalid
argument to unary operator"
There must be a simple way to delete all of the columns, but I just
don't get it.
Thanks for your help,
--
Anne Skoeries
[[alternative HTML version deleted]]
Dear Anne, Check out http://www.nabble.com/correct-way-to-subset-a-vector-to24412577.html#a24412577 for alternatives. HTH, Jorge On Thu, Jul 23, 2009 at 9:49 AM, Anne Skoeries <home@anne-skoeries.de>wrote:> Hi there, > > I'm trying to remove multiple columns by name from a data.frame. As a > result I need to get back the modified data.frame without the removed > columns. My columns I want to delete are listed in a vector called > "delete". > > data <- read.csv2("data.csv") > delete <- c("col1", "col2", "col3") > > newData <- subset(data, select = delete) > newData <- data[delete] > > If I try one of the above shown solutions, it only gives me back the > columns I want to delete. But I need the exact opposite. > > newData <- subset(data, select = -delete) > newData <- data[-delete] > > But as I try the way with a minus I'll get the message: "invalid > argument to unary operator" > > There must be a simple way to delete all of the columns, but I just > don't get it. > Thanks for your help, > -- > Anne Skoeries > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
There are several ways to do it. Here's one:
fakedata <- data.frame(col1 = runif(10), col2 = runif(10), col3 = runif(10))
delete <- c("col1", "col3")
fakedata2 <- fakedata[, !(colnames(fakedata) %in% delete), drop=FALSE]
Sarah
On Thu, Jul 23, 2009 at 9:49 AM, Anne Skoeries<home at anne-skoeries.de>
wrote:> Hi there,
>
> I'm trying to remove multiple columns by name from a data.frame. As a
> result I need to get back the modified data.frame without the removed
> columns. My columns I want to delete are listed in a vector called
> "delete".
>
--
Sarah Goslee
http://www.functionaldiversity.org
try this: data <- data[, !(names(data) %in% delete)] On Thu, Jul 23, 2009 at 9:49 AM, Anne Skoeries<home at anne-skoeries.de> wrote:> Hi there, > > I'm trying to remove multiple columns by name from a data.frame. As a > result I need to get back the modified data.frame without the removed > columns. My columns I want to delete are listed in a vector called > "delete". > > data <- read.csv2("data.csv") > delete <- c("col1", "col2", "col3") > > newData <- subset(data, select = delete) > newData <- data[delete] > > If I try one of the above shown solutions, it only gives me back the > columns I want to delete. But I need the exact opposite. > > newData <- subset(data, select = -delete) > newData <- data[-delete] > > But as I try the way with a minus I'll get the message: "invalid > argument to unary operator" > > There must be a simple way to delete all of the columns, but I just > don't get it. > Thanks for your help, > -- > Anne Skoeries > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?