[R] Deleting a column in a dataframe by name

Rolf Turner r.turner at auckland.ac.nz
Tue Sep 29 23:36:57 CEST 2009


On 30/09/2009, at 10:25 AM, Duncan Murdoch wrote:

> On 29/09/2009 4:54 PM, Rolf Turner wrote:
>> On 30/09/2009, at 9:32 AM, milton ruser wrote:
>>
>>> x=runif(12)
>>> y=runif(12)
>>> w=runif(12)
>>>
>>> mydf<-data.frame(cbind(x,y,w))
>>> head(mydf)
>>>
>>> mydf<-subset(mydf, select=c(-x,-w))
>>> head(mydf)
>>>
>>>
>> 	But this doesn't work if NAME1 and NAME2 are ***names***,
>> 	as the terminology would apply.  And that is precisely the
>> 	situation in which one would wish to apply this sort of
>> 	technique.
>
> I'm not completely sure what you're getting at here, but I think  
> milton
> is right in this example.  (The original one was missing the "c".)

	milton is right if you explicitly type in the names of the
	columns to be deleted.  But it doesn't work if you store
	the names of the columns as character strings.  That's what
	you would have to do when writing code to handle complex
	situations where you didn't know that column names in advance.

	And if you're not writing code, well why not just do something
	simple like mydf[,2] ???
>
> subset() is weird, it does very strange stuff in the select argument.


	Truer words were never spoken/written.

		cheers,

			Rolf

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}




More information about the R-help mailing list