[R] Removing a single element from an array
David Winsemius
dwinsemius at comcast.net
Sun Sep 18 17:50:20 CEST 2011
On Sep 18, 2011, at 11:10 AM, David Winsemius wrote:
>
> On Sep 18, 2011, at 10:47 AM, Dennis Fisher wrote:
>
>> R 2.13.1
>> OS X
>>
>> Colleagues
>>
>> I frequently encounter a situation in which I want to remove a
>> single element of an array. For example, if I am reading in a
>> bunch of CSV files, I create the list of files to be read with:
>> LIST <- dir()
>> However, sometimes I want to exclude one or more files. I can
>> accomplish this with a second command:
>> LIST <- LIST[LIST != "filename.to.be.excluded"]
>> This is cumbersome -- is there some more efficient code to
>> accomplish this?
>
> Doesn't negative indexing provide this?
>
> ?Extract
The most general way to convert character vectors to numbers for this
purpose is with grep which therefore supports regex patterns:
LIST <- dir(path="~/")
> length(LIST)
[1] 551
> LIST[1]
[1] "_train_1.dat"
> LIST <- LIST[ -grep("_train_1.dat", LIST) ]
> length(LIST)
[1] 550
Patterns can also be used in the dir() call.
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list