# Tom Blum # # This program introduces the concept of vectors # instead of having a bunch of atomic weight variables # e.g. c=12.011, h=1.008, etc # we can have a vector with the atomic weights for # carbon, hydrogen, nitrogen and oxygen -- in that order atomic_wt <- c(12.011, 1.008, 14.007, 15.999) # # the c function c() seen above creates a vector # and in R is known as "concatenting" # # Next we can mke another vector with the "subscripts" # for caffeine C8H10N4O2 # caff_subsc <- c(8, 10, 4, 2) # # the product of the two vectors will multiply 12.011 by 8, # 1.008 by 10, etc. # caff_at_wt = caff_subsc * atomic_wt caff_at_wt # # if we sum all of these up we have the molecular # weight of caffeine # caff_mole_wt = sum(caff_at_wt) caff_mole_wt # # we can find the percentage of each atom by weight # by dividing the weights for each element by the # overall weight nd multiplying by 100 # caff_perc_wt = caff_at_wt / caff_mole_wt * 100 caff_perc_wt