Hello, I have two data frames, NameAndConc.df and WhichOnes.df. NameAndConc.df has two columns, the first column is the names of some material, and the second column is the concentration of active ingredient.> NameAndConc.dfname conc 1 material1 0.8 2 material2 0.5 3 material3 0.4 WhichOnes.df has two columns, each of which specifies which material.> WhichOnes.dfcomponent1 component2 1 material2 material3 2 NA material1 3 material3 material2 4 material2 NA>From these two data.frames, I'd like to generate athird data frame that is WhichOnes.df with the names replaced by the corresponding concentrations from NameAndConc.df. So it would be> New.dfcomponent1 component2 1 0.5 0.4 2 NA 0.8 3 0.4 0.5 4 0.5 NA Would someone please lend me a help'n hand with this? Cheers, Dennis __________________________________________________ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 18 Jan 2002, Dennis L. Malandro wrote:> Hello, > > I have two data frames, NameAndConc.df and > WhichOnes.df. NameAndConc.df has two columns, the > first column is the names of some material, and the > second column is the concentration of active > ingredient. > > > NameAndConc.df > name conc > 1 material1 0.8 > 2 material2 0.5 > 3 material3 0.4 > > WhichOnes.df has two columns, each of which specifies > which material. > > > WhichOnes.df > component1 component2 > 1 material2 material3 > 2 NA material1 > 3 material3 material2 > 4 material2 NA > > From these two data.frames, I'd like to generate a > third data frame that is WhichOnes.df with the names > replaced by the corresponding concentrations from > NameAndConc.df. So it would be > > > New.df > component1 component2 > 1 0.5 0.4 > 2 NA 0.8 > 3 0.4 0.5 > 4 0.5 NA > > > Would someone please lend me a help'n hand with this?You need match(). match(New.df$component1,Nameandconc.df$name) gives the row numbers in Nameandconc.df corresponding to the names of component 1. So Nameandconc.df$conc[match(New.df$component1,Nameandconc.df$name)] gives the concentrations and to do them all at once apply(New.df, 2, function(this.component) Nameandconc.df$conc[match(this.component, Nameandconc.df$name)]) -thomas Thomas Lumley Asst. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Dennis L. Malandro" <nawlnz at yahoo.com> writes:> Hello, > > I have two data frames, NameAndConc.df and > WhichOnes.df. NameAndConc.df has two columns, the > first column is the names of some material, and the > second column is the concentration of active > ingredient. > > > NameAndConc.df > name conc > 1 material1 0.8 > 2 material2 0.5 > 3 material3 0.4 > > WhichOnes.df has two columns, each of which specifies > which material. > > > WhichOnes.df > component1 component2 > 1 material2 material3 > 2 NA material1 > 3 material3 material2 > 4 material2 NA > > From these two data.frames, I'd like to generate a > third data frame that is WhichOnes.df with the names > replaced by the corresponding concentrations from > NameAndConc.df. So it would be > > > New.df > component1 component2 > 1 0.5 0.4 > 2 NA 0.8 > 3 0.4 0.5 > 4 0.5 NA > > > Would someone please lend me a help'n hand with this? >Let's see... First of all, the structure of NameAndConc.df is getting in the way since the name field is a key, not really a variable, so try a named vector instead: conc <- NameAndConc.df$conc names(conc) <- NameAndConc.df$name Then use the columns of WhichOnes.df for indexing: New.df <- data.frame(component1=conc[as.character(WhichOnes.df$component1)], component2=conc[as.character(WhichOnes.df$component2)]) or, New.df <- lapply(WhichOnes.df, function(x)conc[as.character(x)]) ..something like that. I suppose you could use match() instead of the character indexing. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Maybe Matching Threads
- problem with se.contrast()
- Finding Interaction and main effects contrasts for two-way ANOVA
- advice on module organization
- finite mixture model (2-component gaussian): plotting component gaussian components?
- [LLVMdev] Creating and building LLVM projects with Eclipse CDT on Windows?