I have a dataframe and some of the columns begin with 'dfall$PROFESSION' which I want to delete. I.e.:
"dfall$PROFESSIONBusinessman" "dfall$PROFESSIONDoctor"
[35] "dfall$PROFESSIONEngineer" "dfall$PROFESSIONFarmer"
[37] "dfall$PROFESSIONHousewife" "dfall$PROFESSIONLawyer"
[39] "dfall$PROFESSIONMissing" "dfall$PROFESSIONPensioner"
So I tried:
names(df_all) <- gsub("dfall$PROFESSION", "", names(df_all))
However, this does not effect any change. Can you explain it? What should I do instead?
Your advice will be appreciated.
You can change the names with gsub
but you must also save them back into the data.frame.
colnames(dfall) = gsub("PROFESSION", "", colnames(dfall))
You can't get rid of the dfall$
part. That is not really part of the column name, rather dfall$PROFESSIONEngineer
specifies the PROFESSIONEngineer
column of the dfall
data.frame .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With