How can I engineer a new feature representing the count of each passenger class and add it to the original dataframe?
You can obtain the class-wise counts using a groupby+aggregate operation with the dplyr
package:
c <- Titanic %>% group_by(Class) %>% summarise(count=n())
You can merge this new dataframe to the Titanic one like this:
merge(x = Titanic, y = c, by = "Class", all.x = TRUE)
This will ensure that the group counts are replicated for all appropriate records in the Titanic dataframe.