My dataframe df is District State Mode
A State1 Cycle
D State2 Car
A State1 Cart
C State1 Walk
B State1 Car
I want to count the modes of transport in each district.
I want the output like this:
District State Cycle Car Cart Walk
A State1 1 0 1 0
B State1 0 1 0 0
C State1 0 0 0 1
D State2 0 1 0 0
I tried using aggregate and table.
grouped_data <- aggregate(df, by=list(df$district, df$Mode,df$State), FUN=length)
grouped_data<-as.data.frame(table(df))
But it is not giving me the desired output.I also would like the percentages for each mode to be displayed as separate columns.