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
3
votes
3 answers

What is the simplest way to recode a variable based on conditions of another variable in R?

Silly example df, "cat": species color tail_length calico brown 6 calico gray 6 tabby multi 5 tabby brown 5 Suppose I want to create a new variable, personality. The values here will be recoded based on tail_length, but will…
poeticpersimmon
  • 179
  • 1
  • 3
  • 8
3
votes
1 answer

Change the coding of two variables in two data.tables and then merge data.table

I have two data.tables ("even" and "odd"), each with the variable "time" (1:6). I want to recode the two "time" variables, so that "time" in "even" has only even numbers and "time" in "odd" has only odd numbers. This is what I have done so far:…
Rebecca
  • 55
  • 4
3
votes
1 answer

Recoding an arbitrary grouping variable or factor in R

Suppose I have a vector or column of arbitrary length representing some grouping/factor variable with an arbitrary number of groups and arbitrary values for same along the lines of this: a <- c(2,2,2,2,2,7,7,7,7,10,10,10,10,10) a [1] 2 2 2 2 2 …
Alexis
  • 784
  • 8
  • 32
2
votes
3 answers

How to recode a range of values (numeric) into a factor variable?

I want to recode a variable that has values from 1 to 10 (numeric) into a factor variable of 5 values. I'm using the command dplyr::recode, but it is not working. I know how to do it with the cut function or case_when, but i want to know if there is…
Renato
  • 23
  • 3
2
votes
4 answers

Recoding continuous variable

I have a continuous variable. Entries 1-60 need to stay the same. NAs and 0s are coded as a number above 60.
exumablue
  • 21
  • 2
2
votes
3 answers

How to make all values in a row after a specific value appears, NA in R

I have data in R that looks like this: T1 <- c(0,0,0,0,0) T2 <- c(1,0,0,0,0) T3 <- c(0,1,0,0,0) T4 <- c(1,1,0,NA,1) T5 <- c(0,1,0,NA,0) df <- data.frame(T1,T2,T3,T4,T5) T1 T2 T3 T4 T5 1 0 1 0 1 0 2 0 0 1 1 1 3 0 0 0 0 0 4 0 0 0…
user21027866
  • 129
  • 5
2
votes
2 answers

How can I create a day number variable in R based on dates?

I want to create a variable with the number of the day a participant took a survey (first day, second day, thirds day, etc.) The issue is that there are participants that took the survey after midnight. For example, this is what it looks…
MAIMAU
  • 31
  • 3
2
votes
1 answer

debug created function using case_when in R

From the original dataframe: id tru wex kjd 1 101 2 2 2 2 106 2 2 3 107 0 0 0 4 110 2 0 5 115 2 2 2 6 118 0 0 Would like to get this dataframe: id tru wex kjd tru_new wex_new…
seehorse
  • 53
  • 5
2
votes
2 answers

R function to recode values or similar (like SPSS)

I have a dataframe with this structure: id v1 v2 v3 v4 v5 1 . 1 . 2 3 2 1 2 3 . . 3 3 2 . 1 . And I want to recode it like this id v1 v2 v3 v4 v5 1 0 3 0 2 1 2 3 2 1 0 0 3 1 2 0 3 0 So I want to recode values (1=3)…
EGM8686
  • 1,492
  • 1
  • 11
  • 22
2
votes
1 answer

How can I recode matchup values in R that are not consistent throughout the dataset?

I have a dataset containing team (roster_id), who that team played (matchup_id), how many points the team (roster_id) scored, which week it occurred (Week), and what season the matchup occurred (Season). The roster_id values are all the individual…
jgsnow
  • 33
  • 3
2
votes
1 answer

AWK merging col1 with col2

say I have a file (file1 - 3 cols) and a cross reference file (xref - 2 cols) and I want to recode all 3 cols of file1 using col1 as key of the cross ref file. file1 1 4 5 2 3 1 3 4 2 xref 1 11 2 21 3 31 4 41 5 51 I can recode the first col of file1…
Mr T.
  • 95
  • 1
  • 1
  • 9
2
votes
1 answer

Using Vectors in recode function

I am a fairly new user of R. I am trying to recode a new variable that takes a particular value based on the value of another variable in the dataset. It is taking a lot of time to assign a value element-wise. To give more context, the Question…
2
votes
2 answers

Recoding categorical variable based on the value of another categorical variable in r data frame

I want to be able to recode Var1 to have the same value of Var2 (if different). Please note that I want to keep the value of Var1 if Var2 is NA. Any help would be appreciated. I do not mind have Var3 with the same outcome. Var1 <- c("A", "A", "D",…
2
votes
3 answers

Why do conditions with %in% ignore missing values?

I encountered an unexpected output when I used %in% in a condition whilst recoding a categorical variable. When an element of a vector on the left is NA, the condition evaluates as FALSE, whilst I expected it to be NA. The expected behaviour is the…
Andrea M
  • 2,314
  • 1
  • 9
  • 27
2
votes
2 answers

recoding dates if larger than?

I have a few dates, and all that happened after 2005-06-12 should be recoded to 2005-06-12 # Example how the data looks like: data <- data.frame(review_date = as.Date(c("2006-04-11","2001-01-01", "2005-06-12", "2007-07-09"))) Basically, desired…
Irma
  • 53
  • 5