dunner wrote on 09/28/2011 08:43:32 AM:
> 
> Please help with this error message 
> 
> drugbook is an 885 x 32 dataframe
> 
>  >names(drugbook)
> 
> [1] "DRUG1"      "DRUG2"      "DRUG3"      "DRUG4"      "DRUG5" 
>  [6] "DRUG6"      "DRUG7"      "DRUG8"      "DRUG9"      "DRUG10" 
> [11] "DRUG11"     "DRUG12"     "DRUG13"     "DRUG14"     "DRUG15" 
> [16] "DRUG16"     "DrugDose1"  "DrugDose2"  "DrugDose3"  "DrugDose4" 
> [21] "DrugDose5"  "DrugDose6"  "DrugDose7"  "DrugDose8"  "DrugDose9" 
> [26] "DrugDose10" "DrugDose11" "DrugDose12" "DrugDose13" "DrugDose14"
> [31] "DrugDose15" "DrugDose16"
> 
> Equivs is a dataframe holding information for a certain subclass of 
drugs
> 
> > head(Equivs)
>               Name  Equiv
> 1       Alprazolam   0.5
> 2       Bromazepam   5.5
> 3 Chlordiazepoxide  25.0
> 4         Clobazam  20.0
> 5       Clonazepam   0.5
> 6      Clorazepate  15.0
> 
> What I'm trying to is for each element of drugbook, get the value in
> Equivs$Equiv, and multiply it by the corresponding drug dose, which will 
be
> in the n+16 th  column on in the same row of drugbook.
> 
> Not all of the grabbed names from Drugbook will be in the dataframe 
Equivs,
> though. So sometimes the == will return a null.
> 
> i.e. DrugDose16 is the dose of Drug 16
> 
> this I think should work: ( I did try apply and got knotted, so stopped)
> 
> >mydosequivalents=list()
> >for( c in 1:16) {
>   for (r in 1:885){
>     name<-as.character(drugbook[r,c]);
>     equivalent<-Equivs$Equiv[Equivs$Name==name];
>     dosequivalent<-drugbook[r,c+16] * equivalent;
>     if (length(dosequivalent)>0) mydosequivalents[r][c]<-dosequivalent
>     else (mydosequivalents[r][c]<-0)
>   }
> }
> 
> But it is throwing an error, and I can't figure out why. I remedied a
> previous error by checking the length of the equivalent.


I think your error is coming from the way that you are trying to populate 
the list using [r][c].  Are you trying to have mydosequivalents be a list 
of r vectors each with c elements?


> >warnings() ....
> 50: In mydosequivalents[c][r] <- dosequivalent :
>   number of items to replace is not a multiple of replacement length
> 
> mydosequivalents ends up being full of "NULL" and zeros
> 
> 
> Thanks in advance for any help
> 
> Ross


Another way:

# using my example data
drugbook <- data.frame(DRUG1=letters[1:5], DRUG2=letters[6:10], 
        DrugDose1=1:5, DrugDose2=6:10)
Equivs <- data.frame(Name=letters[-c(2, 8)], Equiv=100*(1:24))

mydosequivalents <- apply(drugbook[, 1:2], 2, 
        function(x) Equivs$Equiv[match(x, Equivs$Name)])*drugbook[, 3:4]
mydosequivalents[is.na(mydosequivalents)] <- 0


# this should work for your real data
mydosequivalents <- apply(drugbook[, 1:16], 2, 
        function(x) Equivs$Equiv[match(x, Equivs$Name)])*drugbook[, 17:32]
mydosequivalents[is.na(mydosequivalents)] <- 0

Jean
	[[alternative HTML version deleted]]

