[R] Filling a variable with unmentioned categories

jim holtman jholtman at gmail.com
Sat Nov 19 01:05:50 CET 2011


will this work for you:

> str(x)
'data.frame':   10 obs. of  2 variables:
 $ price    : chr  "0,00" "0,00" "0,02" "0,03" ...
 $ mentioned: int  1 1 1 1 1 1 1 1 1 1
> # create vector with range of 'price'
> allPrices <- paste('0,', sprintf("%02d", 0:19), sep = '')
> # find missing prices and add with mentioned = 0
> newX <- rbind(x
+     , data.frame(price = setdiff(allPrices, x$price)
+                 , mentioned = 0
+                 , stringsAsFactors = FALSE
+                 )
+     )
> newX[order(newX$price),]
   price mentioned
1   0,00         1
2   0,00         1
11  0,01         0
3   0,02         1
4   0,03         1
5   0,03         1
6   0,04         1
12  0,05         0
13  0,06         0
14  0,07         0
15  0,08         0
16  0,09         0
7   0,10         1
17  0,11         0
18  0,12         0
19  0,13         0
20  0,14         0
21  0,15         0
8   0,16         1
22  0,17         0
23  0,18         0
9   0,19         1
10  0,19         1
>


On Fri, Nov 18, 2011 at 12:05 PM, Milan Bouchet-Valat <nalimilan at club.fr> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list