I have a data.frame in R which is in long format.
Gender | Area | Value | Year | Column X | Column Y | And other columns... |
---|---|---|---|---|---|---|
male | urban | 31.45 | 2012 | xxx | yyy | zzz |
male | rural | 41 | 2012 | xxx | yyy | zzz |
male | rural | 35.6 | 2012 | xxx | yyy | zzz |
female | rural | 30 | 2012 | xxx | yyy | zzz |
female | urban | 21 | 2016 | xxx | yyy | zzz |
female | rural | 19 | 2016 | xxx | yyy | zzz |
The main aim is to add a new column, say seg to this data.frame whose rows are filled in the following format:
Gender | Area | Value | Year | Column X | Column Y | And other columns... | seg |
---|---|---|---|---|---|---|---|
male | urban | 31.45 | 2012 | xxx | yyy | zzz | {"Gender": "male", "Area": "urban", "Year": 2012} |
male | rural | 41 | 2012 | xxx | yyy | zzz | {"Gender": "male", "Area": "rural", "Year": 2012} |
male | rural | 35.6 | 2012 | xxx | yyy | zzz | {"Gender": "male", "Area": "rural", "Year": 2012} |
female | rural | 30 | 2012 | xxx | yyy | zzz | {"Gender": "female", "Area": "rural", "Year": 2012} |
female | urban | 21 | 2016 | xxx | yyy | zzz | {"Gender": "female", "Area": "urban", "Year": 2016} |
female | rural | 19 | 2016 | xxx | yyy | zzz | {"Gender": "female", "Area": "rural", "Year": 2016} |
The format of the contents of the seg column is important because it will be fed to a database.
Can anyone advise me how exactly to proceed with that in R? I have spent quite sometime looking around for solutions and couldn't find anything that takes column names, row values in a data.frame and converts them into json strings. The manipulation is not on all the columns but only for specific columns.
Appreciate the help in advance!