[R] delete a entire vector of a dataframe
Gavin Simpson
gavin.simpson at ucl.ac.uk
Thu Sep 21 21:28:10 CEST 2006
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
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
*Note new Address and Fax and Telephone numbers from 10th April 2006*
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson [t] +44 (0)20 7679 0522
ECRC [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK [w] http://www.ucl.ac.uk/~ucfagls/cv/
WC1E 6BT [w] http://www.ucl.ac.uk/~ucfagls/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list