[R] a workaround for indexing a function?

David Winsemius dwinsemius at comcast.net
Thu Jun 19 02:03:09 CEST 2014


See this?

	`[[alternative HTML version deleted]]`

Means your efforts at including data have been sabotaged by failing to read the Posting guide and send plain text.

Then this line:    median[i] = median(normHL)

Gets you:
> Error in median[i] = median(normHL) :
> 
>  object of type 'closure' is not subsettable

Were you under the impression that assignment to an indexed vector with a name would succeed when the vector had not already been pre-defined? R was interpreting that as assignment to an object named 'median' which it was able to find. The error was thrown because the located object is a function (aka closure).

Better would be to chose a more specific object name and not one shared by a regular function. That also means you would have gotten the same error with objects named `df` (the density of the F distribution), `dt` (the t-distribution), `c` (the concatenation function), `C` (the contrasts function), `D` (the derivative function)...... getting the idea that using one or two letter names might be fraught with confusion?

Try using 

 nor_med <- numeric(0)  # before the loop and then in the loop use:

  nor_med[i] = median(normHL)

-- 
david.

On Jun 18, 2014, at 10:26 AM, Adam Kustka wrote:

> I noticed a missing line in the code associated with my question.  I've
> re-supplied the question now with that line.
> 
> Sorry for the trouble,
> Adam
> 
> ************
> 
> 
> 
> I have a dataset of peptide 15N/14N ratios from four experiments (where
> cells are grown under different conditions and were fed either 15N or 14N
> nitrate aka plant food if you are interested) and I want to normalize each
> of these ratios to the median 15N/14N value from all >6000 peptides from
> each experiment.  Then, since these ratios (from 2 or more peptides) are
> used to calculate the protein 15N/14N abundance, I want to take the median
> value of all peptides belonging to one protein to calculate the overall
> protein 15N/14N abundance within that experiment.
> 
> 
> 
> I’ve managed to break the data set into small pieces (by protein and by
> experiment), which will lead to about 500 observations.
> 
> 
> 
> A dummy – small – dataset could look like this (below), where V1 lists the
> peptides that make up a protein, V2 is the protein ID, V3 is the metric of
> interest (the 15N/14N ratio of each peptide), and V4 indicates which
> experiment was run.
> 
> 
> 
> The stumbling block here is that I cannot index the median function.
> 
> 
> 
> Here is what I have done so far.
> 
> 
> 
> y<-read.table(“nameoffile.txt”)
> 
> 
> 
> h<-split(y,y$V4)
> 
> 
> 
> for(n in names(h))
> 
> write.table(h[[n]], file=paste(y[[n]][,”V4”][4],”_”,n,”.TXT”,sep=””))
> 
> 
> 
> Then I (not elegantly but OK with brute force method for four files) went
> into each file and calculated the median
> 
> 
> 
> 
> 
> vls1<-read.table(“VLS5584_VLS5584.TXT”)  # for example, but did this four
> times for each file
> 
> 
> 
> newcol=vls1[,3]/median(vls1[,3]) #make a new column of 15N/14N data now
> normalized to median value… here this is shown for one of four experiments.
> 
> 
> 
> 
> normvls1=cbind(vls1,newcol) #added to existing data with global median
> normalized numbers.
> 
> 
> 
> nvls1prot<-split(normvls1,normvls1$V2) #splits up this experiment into many
> files.. one for each protein.
> 
> 
> 
> for(n in
> names(nvls1prot))write.table(nvls1prot[[n]],file=paste(nvls1prot[[n]][,"V4"][1],"_",
> n,".TXT",sep=""))  #writes these files
> 
> 
> 
> Then I took this output in windows explorer and moved it to a new folder.
> Set working directory to that folder.
> 
> 
> 
> 
> 
> And, lifted from a help page and tweaked to my liking..
> 
> 
> 
> # batch import text files (files must be in working directory);
> 'pattern' is case-sensitive
> 
> txtfiles = list.files(pattern="*.TXT")
> 
> 
> 
> for (i in 1:length(txtfiles)){
> 
>  tmp = read.table(txtfiles[i])
> 
>  xyz = data.frame(tmp)
> 
>  normHL = xyz[[5]]
> 
>    median[i] = median(normHL)
> 
> }
> 
> 
> 
> 
> 
> But after that, I get the following error message.
> 
> 
> 
> Error in median[i] = median(normHL) :
> 
>  object of type 'closure' is not subsettable
> 
> 
> 
> Thanks,
> 
> Adam Kustka
> 
> 
> 
> SEAEFVNYQVR
> 
> B5YLU3
> 
> 0.226
> 
> VLS5584
> 
> LLQTEPGTR
> 
> B5YLU3
> 
> 0.199
> 
> VLS5584
> 
> SEAEFVNYQVR
> 
> B5YLU3
> 
> 0.216
> 
> VLS5585
> 
> LLQTEPGTR
> 
> B5YLU3
> 
> 0.183
> 
> VLS5585
> 
> SEAEFVNYQVR
> 
> B5YLU3
> 
> 0.266
> 
> VLS5586
> 
> FAQMAVLGFIIPEK
> 
> B5YLU3
> 
> .2
> 
> VLS5586
> 
> LLQTEPGTR
> 
> B5YLU3
> 
> 0.203
> 
> VLS5586
> 
> SEAEFVNYQVR
> 
> B5YLU3
> 
> 0.516
> 
> VLS5587
> 
> FAQMAVLGFIIPEK
> 
> B5YLU3
> 
> 0.764
> 
> VLS5587
> 
> IEGLGWRPK
> 
> B5YLU3
> 
> .2
> 
> VLS5587
> 
> LLQTEPGTR
> 
> B5YLU3
> 
> 0.338
> 
> VLS5587
> 
> LLQTEPGTR
> 
> B5YLU3
> 
> .2
> 
> VLS5587
> 
> FSCAYLVDNPR
> 
> B5YLV4
> 
> 0.131
> 
> VLS5584
> 
> ITCQDPSDDFPTYR
> 
> B5YLV4
> 
> 0.122
> 
> VLS5584
> 
> SPLPEDLMPEFSFR
> 
> B5YLV4
> 
> 0.105
> 
> VLS5584
> 
> ITCQDPSDDFPTYR
> 
> B5YLV4
> 
> .2
> 
> VLS5585
> 
> FLEVPMYLK
> 
> B5YLV4
> 
> .2
> 
> VLS5585
> 
> ITCQDPSDDFPTYR
> 
> B5YLV4
> 
> 0.113
> 
> VLS5585
> 
> ITCQDPSDDFPTYR
> 
> B5YLV4
> 
> 0.112
> 
> VLS5586
> 
> FSCAYLVDNPR
> 
> B5YLV4
> 
> 0.128
> 
> VLS5586
> 
> SPLPEDLMPEFSFR
> 
> B5YLV4
> 
> 0.126
> 
> VLS5586
> 
> FLEVPMYLKCLDR
> 
> B5YLV4
> 
> 0.406
> 
> VLS5586
> 
> LYYNVETLK
> 
> B5YLV4
> 
> 0.11
> 
> VLS5586
> 
> GEDGYLTTK
> 
> B5YLV4
> 
> 0.144
> 
> VLS5586
> 
> FLEVPMYLK
> 
> B5YLV4
> 
> 0.134
> 
> VLS5586
> 
> ITCQDPSDDFPTYR
> 
> B5YLV4
> 
> .2
> 
> VLS5586
> 
> FSCAYLVDNPR
> 
> B5YLV4
> 
> .2
> 
> VLS5586
> 
> FKDDFFLK
> 
> B5YLV4
> 
> 0.121
> 
> VLS5586
> 
> ITCQDPSDDFPTYRDLEK
> 
> B5YLV4
> 
> 0.138
> 
> VLS5586
> 
> FLEVPMYLK
> 
> B5YLV4
> 
> 0.123
> 
> VLS5586
> 
> CFDDAFVR
> 
> B5YLV4
> 
> 0.15
> 
> VLS5586
> 
> VFFTHGMYYTGGNLVAQVK
> 
> B5YLV4
> 
> 0.25
> 
> VLS5586
> 
> FKDDFFLK
> 
> B5YLV4
> 
> 0.141
> 
> VLS5586
> 
> CGFDEVHAR
> 
> B5YLV4
> 
> 0.108
> 
> VLS5586
> 
> YPENYNIEELPAAGQSYIHPDTYVQR
> 
> B5YLV4
> 
> 0.153
> 
> VLS5587
> 
> SPLPEDLMPEFSFR
> 
> B5YLV4
> 
> 0.153
> 
> VLS5587
> 
> FSCAYLVDNPR
> 
> B5YLV4
> 
> 0.137
> 
> VLS5587
> 
> LYYNVETLK
> 
> B5YLV4
> 
> 0.157
> 
> VLS5587
> 
> CGFDEVHAR
> 
> B5YLV4
> 
> 0.145
> 
> VLS5587
> 
> FLEVPMYLK
> 
> B5YLV4
> 
> 0.142
> 
> VLS5587
> 
> 
> 
> 
> On Wed, Jun 18, 2014 at 9:12 AM, Adam Kustka <kustka at andromeda.rutgers.edu>
> wrote:
> 
>> 
>> 
>> I have a dataset of peptide 15N/14N ratios from four experiments (where
>> cells are grown under different conditions and were fed either 15N or 14N
>> nitrate aka plant food if you are interested) and I want to normalize each
>> of these ratios to the median 15N/14N value from all >6000 peptides from
>> each experiment.  Then, since these ratios (from 2 or more peptides) are
>> used to calculate the protein 15N/14N abundance, I want to take the median
>> value of all peptides belonging to one protein to calculate the overall
>> protein 15N/14N abundance within that experiment.
>> 
>> 
>> 
>> I’ve managed to break the data set into small pieces (by protein and by
>> experiment), which will lead to about 500 observations.
>> 
>> 
>> 
>> A dummy – small – dataset could look like this (below), where V1 lists the
>> peptides that make up a protein, V2 is the protein ID, V3 is the metric of
>> interest (the 15N/14N ratio of each peptide), and V4 indicates which
>> experiment was run.
>> 
>> 
>> 
>> The stumbling block here is that I cannot index the median function.
>> 
>> 
>> 
>> Here is what I have done so far.
>> 
>> 
>> 
>> y<-read.table(“nameoffile.txt”)
>> 
>> 
>> 
>> h<-split(y,y$V4)
>> 
>> 
>> 
>> for(n in names(h))
>> 
>> write.table(h[[n]], file=paste(y[[n]][,”V4”][4],”_”,n,”.TXT”,sep=””))
>> 
>> 
>> 
>> Then I (not elegantly but OK with brute force method for four files) went
>> into each file and calculated the median
>> 
>> 
>> 
>> 
>> 
>> vls1<-read.table(“VLS5584_VLS5584.TXT”)  # for example, but did this four
>> times for each file
>> 
>> 
>> 
>> newcol=vls1[,3]/median(vls1[,3])
>> 
>> 
>> 
>> normvls1=cbind(vls1,newcol)
>> 
>> 
>> 
>> nvls1prot<-split(normvls1,normvls1$V2)
>> 
>> 
>> 
>> for(n in
>> names(nvls1prot))write.table(nvls1prot[[n]],file=paste(nvls1prot[[n]][,"V4"][1],"_",
>> n,".TXT",sep=""))
>> 
>> 
>> 
>> Then I took this output in windows explorer and moved it to a new folder.
>> Set working directory to that folder.
>> 
>> 
>> 
>> And, lifted from a help page and tweaked to my liking..
>> 
>> for (i in 1:length(txtfiles)){
>> 
>>  tmp = read.table(txtfiles[i])
>> 
>>  xyz = data.frame(tmp)
>> 
>>  normHL = xyz[[5]]
>> 
>>    median[i] = median(normHL)
>> 
>> }
>> 
>> 
>> 
>> 
>> 
>> But after that, I get the following error message.
>> 
>> 
>> 
>> Error in median[i] = median(normHL) :
>> 
>>  object of type 'closure' is not subsettable
>> 
>> 
>> 
>> Thanks,
>> 
>> Adam Kustka
>> 
>> 
>> 
>> SEAEFVNYQVR
>> 
>> B5YLU3
>> 
>> 0.226
>> 
>> VLS5584
>> 
>> LLQTEPGTR
>> 
>> B5YLU3
>> 
>> 0.199
>> 
>> VLS5584
>> 
>> SEAEFVNYQVR
>> 
>> B5YLU3
>> 
>> 0.216
>> 
>> VLS5585
>> 
>> LLQTEPGTR
>> 
>> B5YLU3
>> 
>> 0.183
>> 
>> VLS5585
>> 
>> SEAEFVNYQVR
>> 
>> B5YLU3
>> 
>> 0.266
>> 
>> VLS5586
>> 
>> FAQMAVLGFIIPEK
>> 
>> B5YLU3
>> 
>> .2
>> 
>> VLS5586
>> 
>> LLQTEPGTR
>> 
>> B5YLU3
>> 
>> 0.203
>> 
>> VLS5586
>> 
>> SEAEFVNYQVR
>> 
>> B5YLU3
>> 
>> 0.516
>> 
>> VLS5587
>> 
>> FAQMAVLGFIIPEK
>> 
>> B5YLU3
>> 
>> 0.764
>> 
>> VLS5587
>> 
>> IEGLGWRPK
>> 
>> B5YLU3
>> 
>> .2
>> 
>> VLS5587
>> 
>> LLQTEPGTR
>> 
>> B5YLU3
>> 
>> 0.338
>> 
>> VLS5587
>> 
>> LLQTEPGTR
>> 
>> B5YLU3
>> 
>> .2
>> 
>> VLS5587
>> 
>> FSCAYLVDNPR
>> 
>> B5YLV4
>> 
>> 0.131
>> 
>> VLS5584
>> 
>> ITCQDPSDDFPTYR
>> 
>> B5YLV4
>> 
>> 0.122
>> 
>> VLS5584
>> 
>> SPLPEDLMPEFSFR
>> 
>> B5YLV4
>> 
>> 0.105
>> 
>> VLS5584
>> 
>> ITCQDPSDDFPTYR
>> 
>> B5YLV4
>> 
>> .2
>> 
>> VLS5585
>> 
>> FLEVPMYLK
>> 
>> B5YLV4
>> 
>> .2
>> 
>> VLS5585
>> 
>> ITCQDPSDDFPTYR
>> 
>> B5YLV4
>> 
>> 0.113
>> 
>> VLS5585
>> 
>> ITCQDPSDDFPTYR
>> 
>> B5YLV4
>> 
>> 0.112
>> 
>> VLS5586
>> 
>> FSCAYLVDNPR
>> 
>> B5YLV4
>> 
>> 0.128
>> 
>> VLS5586
>> 
>> SPLPEDLMPEFSFR
>> 
>> B5YLV4
>> 
>> 0.126
>> 
>> VLS5586
>> 
>> FLEVPMYLKCLDR
>> 
>> B5YLV4
>> 
>> 0.406
>> 
>> VLS5586
>> 
>> LYYNVETLK
>> 
>> B5YLV4
>> 
>> 0.11
>> 
>> VLS5586
>> 
>> GEDGYLTTK
>> 
>> B5YLV4
>> 
>> 0.144
>> 
>> VLS5586
>> 
>> FLEVPMYLK
>> 
>> B5YLV4
>> 
>> 0.134
>> 
>> VLS5586
>> 
>> ITCQDPSDDFPTYR
>> 
>> B5YLV4
>> 
>> .2
>> 
>> VLS5586
>> 
>> FSCAYLVDNPR
>> 
>> B5YLV4
>> 
>> .2
>> 
>> VLS5586
>> 
>> FKDDFFLK
>> 
>> B5YLV4
>> 
>> 0.121
>> 
>> VLS5586
>> 
>> ITCQDPSDDFPTYRDLEK
>> 
>> B5YLV4
>> 
>> 0.138
>> 
>> VLS5586
>> 
>> FLEVPMYLK
>> 
>> B5YLV4
>> 
>> 0.123
>> 
>> VLS5586
>> 
>> CFDDAFVR
>> 
>> B5YLV4
>> 
>> 0.15
>> 
>> VLS5586
>> 
>> VFFTHGMYYTGGNLVAQVK
>> 
>> B5YLV4
>> 
>> 0.25
>> 
>> VLS5586
>> 
>> FKDDFFLK
>> 
>> B5YLV4
>> 
>> 0.141
>> 
>> VLS5586
>> 
>> CGFDEVHAR
>> 
>> B5YLV4
>> 
>> 0.108
>> 
>> VLS5586
>> 
>> YPENYNIEELPAAGQSYIHPDTYVQR
>> 
>> B5YLV4
>> 
>> 0.153
>> 
>> VLS5587
>> 
>> SPLPEDLMPEFSFR
>> 
>> B5YLV4
>> 
>> 0.153
>> 
>> VLS5587
>> 
>> FSCAYLVDNPR
>> 
>> B5YLV4
>> 
>> 0.137
>> 
>> VLS5587
>> 
>> LYYNVETLK
>> 
>> B5YLV4
>> 
>> 0.157
>> 
>> VLS5587
>> 
>> CGFDEVHAR
>> 
>> B5YLV4
>> 
>> 0.145
>> 
>> VLS5587
>> 
>> FLEVPMYLK
>> 
>> B5YLV4
>> 
>> 0.142
>> 
>> VLS5587
>> 
>> 
>> 
>> 
> 
> 	[[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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list