[R] delete a entire vector of a dataframe

Adrian DUSA adi at roda.ro
Fri Sep 22 13:18:11 CEST 2006


This works too:
t.d$V712 <- NULL

On Thursday 21 September 2006 22:28, Gavin Simpson wrote:
> On Thu, 2006-09-21 at 20:01 +0200, Thomas Preuth wrote:
> > delete a entire vector of a dataframe
> >
> > Hello,
> >
> > i want to delete a vector and tried "rm (t.d$V712)". This did not work,
> > message was, could not find variable. I thought the $ defines the vectro
> > in a dataframe, when I just type "t.d$V712" the content of this vector
> > is displayed.
> >
> > Greetings, Thomas
>
> You can't do that, and that is not what the error message said exactly -
> which should have told you something was wrong with your thinking as it
> also said "1: remove: variable "$" was not found". Instead, copy over
> the object, minus the column you want to delete:
>
> dat <- as.data.frame(matrix(rnorm(100), nrow = 10))
> names(dat) <- paste("Var", 1:10, sep = "_")
> dat
> # now we don't want column Var_6
> dat <- dat[, -6]
> # or if we don't know which column is Var_6 you could do
> not.want <- which(names(dat) %in% "Var_7") # now don't want Var_7
> dat <- dat[, -not.want]
> dat
>
> This can be extended to many variables:
>
> not.want <- which(names(dat) %in% c("Var_10", "Var_2", "Var_8"))
> dat <- dat[, -not.want]
> dat # only 1, 3, 4, 5, 9 left
>
> HTH
>
> G

-- 
Adrian DUSA
Arhiva Romana de Date Sociale
Bd. Schitu Magureanu nr.1
050025 Bucuresti sectorul 5
Romania
Tel./Fax: +40 21 3126618 \
          +40 21 3120210 / int.101



More information about the R-help mailing list