[R] number of items to replace is not a multiple of replacement length

dunner ross.dunne at tcd.ie
Wed Sep 28 15:43:32 CEST 2011


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.

>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

--
View this message in context: http://r.789695.n4.nabble.com/number-of-items-to-replace-is-not-a-multiple-of-replacement-length-tp3851265p3851265.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list