I have the following (simplified) vectors: index <- c("shoe" "shirt" "fruit") cost <- c(100, 50, 2) data <- c("shirt", "shoe", "vegetable") I want my outcome to be: (50, 100, 0) (shirt => 50, shoe => 100, vegetable => not found, so 0) I have written the following function: for (i in custom_list) { + this_cost <- cost[index == i] + message(this_cost) + } This gives me (50, 100) I haven't figured out how to use the ifelse. But more importantly, I think there should be an easier, and faster way to do this with vectors? Thanks! Paul [[alternative HTML version deleted]]
> On Apr 15, 2016, at 5:58 PM, Paul Tremblay <paulhtremblay at gmail.com> wrote: > > I have the following (simplified) vectors: > index <- c("shoe" "shirt" "fruit") > cost <- c(100, 50, 2) > data <- c("shirt", "shoe", "vegetable") > > I want my outcome to be: > > (50, 100, 0) > > (shirt => 50, shoe => 100, vegetable => not found, so 0)c(cost,0)[ match(index, data, nomatch = length(index)+1) ] #[1] 50 100 0> > I have written the following function: > > > for (i in custom_list) { > + this_cost <- cost[index == i] > > + message(this_cost) > + } > > > This gives me (50, 100) > > I haven't figured out how to use the ifelse. But more importantly, I think > there should be an easier, and faster way to do this with vectors? > > Thanks! > > Paul > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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
You neglected the commas in your index expression! See ?match as in:> out <- cost[match(data,index)] > out[1] 50 100 NA (and "data" is a bad name to use as there is already a data() function in R). Please DO go through an R tutorial or two to learn about some of these basic, useful R capabilities. There are many good ones on the Web. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Apr 15, 2016 at 5:58 PM, Paul Tremblay <paulhtremblay at gmail.com> wrote:> I have the following (simplified) vectors: > index <- c("shoe" "shirt" "fruit") > cost <- c(100, 50, 2) > data <- c("shirt", "shoe", "vegetable") > > I want my outcome to be: > > (50, 100, 0) > > (shirt => 50, shoe => 100, vegetable => not found, so 0) > > I have written the following function: > > > for (i in custom_list) { > + this_cost <- cost[index == i] > > + message(this_cost) > + } > > > This gives me (50, 100) > > I haven't figured out how to use the ifelse. But more importantly, I think > there should be an easier, and faster way to do this with vectors? > > Thanks! > > Paul > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.