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
1
vote
1 answer

How to shift values left in R so that first non-NA value propagates column 1

I am trying to create a new set of variables based on observations at 5 different time points. However, there is not an observation for each row at each time point. Assuming it looks something like this: X1 <- c(NA,NA,7,8,1,5) X2 <-…
user21027866
  • 129
  • 5
1
vote
2 answers

Recode data frame based on an index column

I have the following data frame df: Index col_a col_b col_c col_d 1 4 c v g j 2 1 x z s 3 1, 3 k j n y 4 2 q t o i 5 2, 3 y m w r 6 2 d u …
benson23
  • 16,369
  • 9
  • 19
  • 38
1
vote
1 answer

How to recode multiple columns that are characters into numeric at once?

I'm new here so apologies if I make a couple mistakes here but essentially, I am trying to recode a lot of columns that have to do with the same content but at different time points. Therefore, I'm trying to find a way to recode multiple columns…
user21027866
  • 129
  • 5
1
vote
1 answer

Recoding a string variable into categories in Python similar to how you can do it in SAS

I have a very long list of string insurance types that I am categorizing. A few are standard, but there is a long tail for which I would like to categorize by the string insurance type containing certain terms. For example, if the string contains…
Sandra T
  • 71
  • 6
1
vote
1 answer

Can you use a for loop within dplyr::case_when()?

I have a named list with N elements. The list-element is used to recode an existing vector. My code is at the moment - hera as an example: vowels<- c("a","e","i","u") consonants <- letters[!(letters %in% vowels)] signs <- c("!",".") # My named list…
1
vote
1 answer

Recode Several Columns Based on Observation

I am using R to recode several columns. Specifically, I have a data frame in which I would like to recode several columns based on whether they are the same value (1) or not (0) based on a specific observation/observation ID value. For example, I…
Dyllan
  • 117
  • 10
1
vote
2 answers

Rename/recode variable value in R based on condition using dplyr

I have a dataset dataExtended with variable CountryOther and n which is a count of wines in that particular country. CountryOther is character type and n is integer. What I want to do, is to rename values in CountryOther to Other in case the n <=20.…
1
vote
1 answer

How do you recode a variable?

I have a variable (animal) that sits in a data frame (data). It is coded 1 = dog, 2 = cat, 3 = bunny, 4 = horse, 5 = monkey. I want to recode it so that horse and bunny = 2 and everything else = 1. How do I do this?
1
vote
3 answers

How to recode multiple columns of one dataframe based on a column from another dataframe?

I want to create new columns in df1 based on two columns in df2. df1 has multiple columns ("x", "y" , "z"), with possible values 0,1, or NA. df2 has two columns: "a" and "b". The values of column "b" include the column names of df1 (although some…
Jaume
  • 189
  • 1
  • 8
1
vote
0 answers

Recode or case_when issue in combining variables using or

I am trying to use recode or case_when to create a subset of my dataset that includes variables that indicate a risk factor for HIV. Essentially, I have four variables that indicate HIV risk and I want to combine them so that I can say if this…
Sabrina
  • 11
  • 1
1
vote
2 answers

Compare across columns return value in new column

So I have a df df <- cbind.data.frame( ID = c("123", "604", "789", "193", "872"), r1 = c("HISPANIC", "WHITE", "ASIAN", "BLACK", "ASIAN"), r2 = c(NA, NA, "WHITE", "HISPANIC", "OTHER"), r3 = c(NA, NA, NA, "OTHER", "OTHER")) …
dlebo
  • 19
  • 2
1
vote
4 answers

How to recode values in a columns sequence in R

How can I recode 0 to 1 and 1 to 0 for columns i1:i3 in the below sample dataset? df <- data.frame(id = c(11,22,33), i1 = c(0,1,NA), i2 = c(1,1,0), i3 = c(0,NA,1)) > df id i1 i2 i3 1 11 0 1 …
amisos55
  • 1,913
  • 1
  • 10
  • 21
1
vote
1 answer

Recode and sum to NA when all values are NA in R

I need to assign NA when all the columns are empty in summation for each id. Here is how my sample dataset looks like; df <- data.frame(id = c(1,2,3), i1 = c(1,NA,0), i2 = c(1,NA,1), i3 =…
amisos55
  • 1,913
  • 1
  • 10
  • 21
1
vote
1 answer

How to recode values in haven_labelled vectors in R

I am working with data imported from SPSS using the haven package, imported using read_sav(). The data exists in columns of class haven_labelled, which is somewhat similar to a factor in that it contains a value and a label but is different in other…
AWaddington
  • 725
  • 8
  • 18
1
vote
1 answer

How to recode only some values in R to NA using dplyr::if_else?

I want to recode values below 0 to NA with dpylr::if_else, leaving all other values unchanged. I know there are other ways to do this, but I can't figure out why this doesn't work: data %>% mutate(x = if_else(x < 0, NA, x)) R returns this: "false…
cory
  • 49
  • 4