My dataframe df is District State Mode
A State1 Cycle
D State2 CarA State1 Cart
C State1 WalkB 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 0B State1 0 1 0 0C State1 0 0 0 1D 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.