Dear All, Here's my problem. I have two lists:> v[[1]] [1] "five" [[2]] [1] "four" [[3]] [1] "three" [[4]] [1] "two" [[5]] [1] "one" [[6]] [1] "six" [[7]] [1] "five" "four" "three" "two" "one" "six" [[8]] [1] "four" "three" "two" "one" "six" [[9]] [1] "three" "two" "one" "six" [[10]] [1] "two" "one" "six" and the other is something like:>valuesnumbers five 50 four 40 three 30 two 10 one 10 six 10>I'm trying to get a list similar to "v", but in which the entries "three", "two", and so on are replaced by the values in the second list. Any help will be greatly appreciated. best regards, Manoel [[alternative HTML version deleted]]
Hi lapply should do it if values is a data frame with first column of names and second column numbers. lapply(v, function(x) values[match(x, values[ ,1]), 2]) Regards Petr r-help-bounces at r-project.org napsal dne 14.04.2009 14:01:50:> Dear All, > > Here's my problem. I have two lists: > > > v > [[1]] > [1] "five" > [[2]] > [1] "four" > [[3]] > [1] "three" > [[4]] > [1] "two" > [[5]] > [1] "one" > [[6]] > [1] "six" > [[7]] > [1] "five" "four" "three" "two" "one" "six" > [[8]] > [1] "four" "three" "two" "one" "six" > [[9]] > [1] "three" "two" "one" "six" > [[10]] > [1] "two" "one" "six" > > > and the other is something like: > > >values > numbers > five 50 > four 40 > three 30 > two 10 > one 10 > six 10 > > > > > > I'm trying to get a list similar to "v", but in which the entries"three",> "two", and so on are replaced by the values in the second list. Any helpwill> be greatly appreciated. > > best regards, > > Manoel > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Try this, numbers <- c("one","two","three","four") values <- c(10,20,30,40) v <- list(sample(numbers,3),sample(numbers,2)) v sapply(v,function(.l) values[match(.l, numbers)] ) HTH, baptiste On 14 Apr 2009, at 13:01, Manoel Silva wrote:> Dear All, > > Here's my problem. I have two lists: > >> v > [[1]] > [1] "five" > [[2]] > [1] "four" > [[3]] > [1] "three" > [[4]] > [1] "two" > [[5]] > [1] "one" > [[6]] > [1] "six" > [[7]] > [1] "five" "four" "three" "two" "one" "six" > [[8]] > [1] "four" "three" "two" "one" "six" > [[9]] > [1] "three" "two" "one" "six" > [[10]] > [1] "two" "one" "six" > > > and the other is something like: > >> values > numbers > five 50 > four 40 > three 30 > two 10 > one 10 > six 10 >> > > > > I'm trying to get a list similar to "v", but in which the entries > "three", "two", and so on are replaced by the values in the second > list. Any help will be greatly appreciated. > > best regards, > > Manoel > > > > [[alternative HTML version deleted]] > > <ATT00001.txt>_____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag