Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Data manipulation is a breeze with amazing packages like plyr and dplyr. Recoding factors, which could prove to be a daunting task especially for variables that have many categories, can easily be accomplished with these packages. However, it is important for those learning Data Science to understand how the basic R works.

In this regard, I seek help from R specialists about recoding factors using the base R. My question is about why one notation in recoding factors works while the other doesn’t. I’m sure for R enthusiasts, the answer and solution are straightforward. So, here’s the question.

In the following code, I generate a vector with five categories and 300 observations. I convert the vector to a factor and tabulate it.

Note that by using as.numeric option, I could see the internal level structure for the respective character notation. Let’s say, I would like to recode categories a and f as missing. I can accomplish this with the following code.

Where 1 and 6 correspond to a and f.

Note that I have used the position of the levels rather than the levels themselves to convert the values to missing.

So far so good.

Now let’s assume that I would like to convert categories a and f to grades. The following code, I thought, would work, but it didn’t. It returns varying and erroneous answers.
Hence the question: Why one method works and the other doesn’t? Looking forward to responses from R experts.

lebatsnok (https://stackoverflow.com/users/2787952/lebatsnok) answered the question on stackoverflow. The solution is simple. The following code works:

The problem with my approach, as explained by lebastsnok, is the following:

“levels(x) is a character vector with length 6, as.numeric(x) is a logical vector with length 300. So you’re trying to index a short vector with a much longer logical vector. In such an indexing, the index vector acts like a “switch”, TRUE indicating that you want to see an item in this position in the output, and FALSE indicating that you don’t. So which elements of levels(x) are you asking for? (This will be random, you can make it reproducible with set.seed if that matters.”
{"ft_lang_label":"__label__en","ft_lang_prob":0.92365485,"math_prob":0.67645633,"size":2820,"snap":"2021-31-2021-39","text_gpt3_token_len":629,"char_repetition_ratio":0.10546875,"word_repetition_ratio":0.094936706,"special_character_ratio":0.21382979,"punctuation_ratio":0.1184669,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98342437,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-16T10:02:46Z\",\"WARC-Record-ID\":\"<urn:uuid:c83d9d5f-ead5-4c5a-a532-ca38c18a8923>\",\"Content-Length\":\"100158\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0b4458eb-98e6-4cec-814f-6609a3f6d5ba>\",\"WARC-Concurrent-To\":\"<urn:uuid:02b7c6d5-664d-4078-b863-6fcd79380fe4>\",\"WARC-IP-Address\":\"172.64.203.30\",\"WARC-Target-URI\":\"https://www.r-bloggers.com/2018/10/a-question-and-an-answer-about-recoding-several-factors-simultaneously-in-r/\",\"WARC-Payload-Digest\":\"sha1:SYFY5UMJERVOLRLJAGW73LE3O6BMK47F\",\"WARC-Block-Digest\":\"sha1:6R5DNCF46QPKEWU423ZJ3AYJ3P3QM3AV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780053493.41_warc_CC-MAIN-20210916094919-20210916124919-00441.warc.gz\"}"}