[R] Filling a variable with unmentioned categories

Milan Bouchet-Valat nalimilan at club.fr
Fri Nov 18 18:05:40 CET 2011


Le vendredi 18 novembre 2011 à 15:06 +0000, Mario Giesel a écrit :
> Hello, list,
> 
> I've been struggling with this task for a while looking for an efficient way to solve it:
> There are two variables 'price' and 'mentioned'.
> I want to 'enlarge' data so that missing price points within the price range are added to variable price.
> Also variable 'mentioned' is to receive values 0 in these cases.
> Note: Price points in original data can repeat if several persons mentioned that price point.
Let's say prices1 holds the first list of prices, prices2 the
supplementary one. Try:
prices <- c(prices1, prices2)
prices <- sort(prices[!duplicated(prices)])
data.frame(price=prices, mentioned=prices %in% prices1)

Regards



More information about the R-help mailing list