I'm trying to help my friend, Director of Sales, make sense of his logged call data. There is one column in particular in which he is interested, "Disposition". This column has string values and I'm trying to convert them to numeric values (i.e. "Not Answered" converted to 1, "Answered" converted to 2, etc.) and remove any row with no values entered. I've created data frames, used as.numeric, created and deleted columns/rows, etc. to no avail. I'm just trying to run simple R code to give him some insight. Any and all help is much appreciated. Thanks in advance!
P.S. I'm unsure as to whether I should provide some code due to the fact that there is a lot of delicate information (personal phone numbers and emails).
First off: You should always provide representative sample data; if your data is sensitive in nature, provide mock-up data.
That aside, to recode a
character
vector asnumeric
you could convert tofactor
and then useas.numeric
. For example:The numbering can be adjusted by changing the order of the factor
levels
.Alternatively, you can create a new column and fill it with the numeric values using an
ifelse
statement. To illustrate, let's assume this is your dataframe:Now you define a new column, say df$Analysis, and assign to it numbers based on the information in df$Disposition:
The advantage of this method is that you keep the original information unchanged. If you now want to remove Na values in the dataframe, use
na.omit
. NB: this will remove not only the NA values in df$Disposition but any row with NA in any column: