I have a dataframe:
location <- c("a", "b", "c", "d", "e", "e")
type <- c("city", "city", "town", "town", "village", "village")
code <- c("123", "112", "83749", "83465", "38484757", "3838891")
country <- c("zz", "zz", "zz", "zz", "zz", "zz")
df <- data.frame(location, type, code, country)
I want to group by location and convert to dictionary Something like below:
{location:[[type], [code], [country]]}
I know this should be quite straight forward using python, but I am not sure how to do that using R. I have tried below using unclass, but still didn't get what i am expecting:
unclass(by(df, df$location, function(x) {
tmp <- x$code
setNames(tmp, x$location[1])
tmp
})) -> location_mapping
Expected Output:
{
'a':[['city'],['123'],['zz']],
'b':[['city'],['112'],['zz']],
'c':[['town'],['83749'],['zz']],
'd':[['town'],['83465'],['zz']],
'e':[['village'],['38484757','3838891'],['zz']]
}