[R] How to replace missing values by mean of subgroup of a group

Olu Ola oluola2011 at yahoo.com
Tue May 9 21:14:06 CEST 2017


 Hello,I have the following food data with some NA values in the food prices. I will like to replace the NA values in the food price column for each food item by the mean price of the specific food item for each city. For example, the price of bean for the household with hhid 102 in the data set is missing. I will like to replace the missing value with the mean price of bean for the households living in Paxton city (that is households 101 and 103). the data set is presented below. Any help will be greatly appreciated.

| hhid | city | food | food price |
| 101 | Paxton | rice | 10 |
| 101 | Paxton | beans | 30 |
| 101 | Paxton | flour | NA |
| 101 | Paxton | eggs | 20 |
| 102 | Paxton | rice | NA |
| 102 | Paxton | beans | NA |
| 102 | Paxton | flour | 34 |
| 102 | Paxton | eggs | 21 |
| 103 | Paxton | rice | 15 |
| 103 | Paxton | beans | 28 |
| 103 | Paxton | flour | 32 |
| 103 | Paxton | eggs | NA |
| 104 | Hull | rice | NA |
| 104 | Hull | beans | 34 |
| 104 | Hull | flour | NA |
| 104 | Hull | eggs | 24 |
| 105 | Hull | rice | 18 |
| 105 | Hull | beans | 38 |
| 105 | Hull | flour | 36 |
| 105 | Hull | eggs | 26 |
| 106 | Hull | rice | NA |
| 106 | Hull | beans | NA |
| 106 | Hull | flour | 40 |
| 106 | Hull | eggs | NA |


	[[alternative HTML version deleted]]



More information about the R-help mailing list