Converting factor variable into numeric?

Apologies if this is a silly question as I'm still getting to grips with R, but I am analysing specific variables from a dataset for an assignment, and one of the data columns is a factor variable but i want to change it to a numeric variable. This column records the responses to a question as 'trust' or 'no trust'. Is there away i could assign a numeric value to each answer e.g. 0 = no trust / 1 = trust as it would make it easier to interpret the results.

A factor already has numeric values. They are simply displayed with text labels. If your data frame is named DF and your column is named Q1, you can get the numeric values by running

DF$Q1 <- as.numeric(DF$Q1)

You will probably find that "no trust" = 1 and "trust" = 2. You can subtract 1 from the whole column if you want to reset that to 0 and 1.

1 Like

If You have a data frame called DF_1 then what you can do is
DF_1$responses <- ifelse(DF_1$responses=="trust",1,0)
DF_1

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.