Questions tagged [recode]

Recoding refers to the process of modifying the values of dataset, matrix, or vector according to a set of rules by which input values are changed to replacement values.

Recoding refers to the process of modifying the values of dataset, matrix, or vector according to a set of rules by which input values are changed to replacement values.

624 questions
151
votes
10 answers

Replace a value in a data frame based on a conditional (`if`) statement

In the R data frame coded for below, I would like to replace all of the times that B appears with b. junk <- data.frame(x <- rep(LETTERS[1:4], 3), y <- letters[1:12]) colnames(junk) <- c("nm", "val") this provides: nm val 1 A a 2 B …
DQdlM
  • 9,814
  • 13
  • 37
  • 34
25
votes
10 answers

Canonical tidyverse method to update some values of a vector from a look-up table

I frequently need to recode some (not all!) values in a data frame column based off of a look-up table. I'm not satisfied by the ways I know of to solve the problem. I'd like to be able to do it in a clear, stable, and efficient way. Before I write…
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
17
votes
13 answers

Idiom for ifelse-style recoding for multiple categories

I run across this often enough that I figure there has to be a good idiom for it. Suppose I have a data.frame with a bunch of attributes, including "product." I also have a key which translates products to brand + size. Product codes 1-3 are…
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
15
votes
1 answer

dplyr::recode Why does pipe generate error?

If I use recode in a pipe, I get an error: df <- df %>% recode(unit, .missing="g") Error in UseMethod("recode") : no applicable method for 'recode' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')" If I pull it out of the…
datakritter
  • 590
  • 5
  • 19
15
votes
3 answers

Recode a variable using data.table

I am trying to recode a variable using data.table. I have googled for almost 2 hours but couldn't find an answer. Assume I have a data.table as the following: DT <- data.table(V1=c(0L,1L,2L), V2=LETTERS[1:3], …
user3077008
  • 837
  • 4
  • 13
  • 24
13
votes
9 answers

Use recode to mutate across multiple columns using named list of named vectors

I couldn't find a question similar to the one that I have here. I have a very large named list of named vectors that match column names in a dataframe. I would like to use the list of named vectors to replace values in the dataframe columns that…
hvgupta
  • 189
  • 12
10
votes
4 answers

Convert month names to numbers in r

I am using data that includes full month names: months <- c("March", "April", "May", "June", "July", "August", "September") Is there a function that will convert them to numbers? Thank you so much
Sharif Amlani
  • 1,138
  • 1
  • 11
  • 25
9
votes
1 answer

how to pass a named vector or two vectors as arguments to dplyr::recode

I'd like to pass either a named vector or two vectors to dplyr::recode. Let's say I've got: library(dplyr) set.seed(1) x <- c("customer", sample(c("a", "b", "c"), 10, replace = TRUE)) recode_tbl <- tibble(letter = letters[1:3], fruit = c("apple",…
biomiha
  • 1,358
  • 2
  • 12
  • 25
8
votes
5 answers

Recode multiple columns using dplyr

I had a dataframe where I recoded several columns so that 999 was set to NA dfB <-dfA %>% mutate(adhere = if_else(adhere==999, as.numeric(NA), adhere)) %>% mutate(engage = if_else(engage==999, as.numeric(NA), engage)) %>% mutate(quality =…
D. Bontempo
  • 176
  • 1
  • 1
  • 9
7
votes
4 answers

Create a variable with exclusive conditions in R

My data look like this Q3A1<-c(0,1,0,1,1,1,0,1,0,1) Q3A2<-c(0,1,1,1,0,1,0,0,0,1) Q3A3<-c(1,1,0,1,0,1,0,0,0,1) And I would like to create a new variable Q3L were it is coded as 1 when Q1A1, Q1A2 and Q1A3 are all equal to 1 I tried this dataQ$Q3L <-…
Leaves
  • 71
  • 2
7
votes
2 answers

R: How to recode multiple variables at once

I have several variables in my dataset that need to be recoded in exactly the same way, and several other variables that need to be recoded in a different way. I tried writing a function to help me with this, but I'm having…
Bertrand
  • 73
  • 1
  • 1
  • 3
7
votes
3 answers

Recode numeric values in R

I want to recode some numeric values into different numeric values and have had a go using the following code: survey$KY27PHYc <- revalue(survey$KY27PHY1, c(5=3, 4=2,3=2,2=1,1=1)) I get the following error: ## Error: unexpected '=' in…
Ash
  • 237
  • 2
  • 6
  • 16
6
votes
1 answer

Recode a string column into integer using dplyr

How to create a new integer column recode which recodes for an existing column y in the dataframe df using dplyr approaches? # Generates Random data df <- data.frame(x = sample(1:100, 50), y = sample(LETTERS, 50, replace = TRUE),…
Prradep
  • 5,506
  • 5
  • 43
  • 84
5
votes
2 answers

Using purrr to recode across multiple columns with multiple mappings

I have a dataframe with questionnaire response labels. I always like to make a tibble with item-answer definitions and then use dplyr::recode() to replace all item labels with their corresponding definitions. For ease of use the definitions tibble…
Claudiu Papasteri
  • 2,469
  • 1
  • 17
  • 30
5
votes
3 answers

Recoding existing column in R

I have dataframe containing following two columns Tumor_Barcode SEX MEL-JWCI-WGS-1 Male MEL-JWCI-WGS-11 Male MEL-JWCI-WGS-12 Female MEL-JWCI-WGS-13 Male I want to recode column Tumor_Barcode into third column…
Aryh
  • 479
  • 1
  • 4
  • 16
1
2 3
41 42