Replacing words by numbers in multiple columns of a data frame in R
I want to replace the values in a data set (sample in the picture) using numbers instead of words, e.g., 1
instead of D
, -1
instead of R
, 0
for all other values. How can I do it with a loop? I know it can be done doing this instead:
(suppose d
is index name)
d[d$Response == "R",]$Response = -1
d[d$Response == "D",]$Response = 1
... (other values code it and assign value of) = 0