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

Get the most frequent value per row and account for ties

Sample data: df <- data.frame("ID" = 1:6, "Group1" = c("A", NA, "C", NA, "E", "C"), "Group2" = c("E", "C", "C", NA, "E", "E"), "Group3" = c("A", "A", NA, NA, "C", NA), "Group4" =…
Laura
  • 306
  • 3
  • 12
5
votes
3 answers

Conditional recode based on lookup vector

I need to conditionally recode my dataframe d according to a lookup vector. dput(lookup) structure(c("Apple", "Apple", "Banana", "Carrot"), .Names = c("101", "102", "102", "103")) dput(d) structure(list(pat = c(101, 101, 101, 102, 102, 103), gene…
phusion
  • 97
  • 1
  • 9
5
votes
1 answer

Recoding multiple variables using tidyverse in R

May be a silly question, I want to recode multiple variable in a tibble with multiple conditions. Data example: library(tidyverse) s <- matrix(sample(1:15, 20, replace = TRUE), ncol = 4) s <- as_tibble(s) Which gives something like this: # A…
Tito Sanz
  • 1,280
  • 1
  • 16
  • 33
5
votes
1 answer

How to change values that start with a certain letter to NA (in R)

I have a data frame that I'm using called "fish". The data frame has 3 different variables. One of the variables is called "species". There are some species that start with the letter M. I want to change all the values of species that start with the…
newtoallthis
  • 53
  • 1
  • 5
5
votes
1 answer

R data.table multi column recode/sub-assign

Let DT be a data.table: DT<-data.table(V1=sample(10), V2=sample(10), ... V9=sample(10),) Is there a better/simpler method to do multicolumn recode/sub-assign like this: DT[V1==1 | V1==7,V1:=NA] DT[V2==1…
4
votes
3 answers

Replacing predefined values in R

I have a defined character values and I need to replace them in another dataset. Here is the sample dataset data1 <- data.frame(id = c(1,2,3), cat = c("A","B","C")) > data1 id cat 1 1 A 2 2 B 3 3 C data2 <-…
amisos55
  • 1,913
  • 1
  • 10
  • 21
4
votes
3 answers

R Recode variable for all observations that do not occur more than once

I have a simple dataframe that looks like the following: Observation X1 X2 Group 1 2 4 1 2 6 3 2 3 8 4 2 4 1 3 3 5 2 8 4 6 7 5 5 7 2 4 5 How can I…
flâneur
  • 633
  • 2
  • 8
4
votes
3 answers

Replace multiple values across columns in a loop

I want to recode multiple values across different columns. For example: df <- data.frame(wave = c(1,1,1,1,1,1,2,2,2,2,2,2), party = rep(c("A", "A", "A", "B", "B", "B"), 2), s_item = rep(c(3,4,5,1,2,6), 2), …
Joost Maxen
  • 167
  • 8
4
votes
5 answers

how to write a for loop in R that recodes multiple variables?

I am a self-taught programmer with a few years' experience in MATLAB. I'm brand new to R and this is my first question on Stack Overflow. I am trying to recode multiple variables in a dataframe using recode from dplyr. In the code below, I provide a…
4
votes
3 answers

How to automate recoding of many variables using mutate_at and nested ifelse statement?

There is a large data set consisting of repeated measures of the same variable on each subject. An example data is as below df<-data.frame( "id"=c(1:5), "ax1"=c(1,6,8,15,17), "bx1"=c(2,16,8,15,17)) where "x1" is measured repeatedly so we can have…
T Richard
  • 525
  • 2
  • 9
4
votes
1 answer

use mutate_at for variables that meet two criteria dplyr R

I'm trying to reverse score (recode) some items in a dataframe. All reverse scored items end in an R, and each scale has a unique start ("hc", "out", and "hm"). I normally would just select all variables that end with an "r", but the issue is that…
J.Sabree
  • 2,280
  • 19
  • 48
4
votes
1 answer

bunch recoding of variables in the tidyverse (functional / meta-programing)

I want to recode a bunch of variables with as few function calls as possible. I have one data.frame where I want to recode a number of variables. I create a named list of all variable names and the recoding arguments I want to execute. Here I have…
TimTeaFan
  • 17,549
  • 4
  • 18
  • 39
4
votes
1 answer

R: Recoding variables using recode, mutate and case_when

I want to recode the following values < 4 = -1, 4 = 0, > 4 = 1 for the following variables defined by core.vars in the dataset, and still keep the rest of the variables in the data frame. temp.df <- as.tibble (mtcars) other.vars <- c('hp', 'drat',…
KP1
  • 129
  • 2
  • 8
4
votes
1 answer

R: Error while recoding variables using car::recode function

I frequently use 'recode' function in library(car) to recode levels in variables. My code was working fine up until today, but now it is throwing me error. Nothing's changed in df etc, not sure what's going on. May be somebody could enlighten me! My…
Rudr
  • 387
  • 4
  • 20
4
votes
2 answers

How to use recode_factor in dplyr for recoding multiple factor values?

countrycode event 1713 ESP 110mh 1009 NED HJ 536 BLR LJ 2882 FRA 1500m 509 EST LJ 2449 BEL PV 1022 EST HJ 2530 USA JT 2714 CUB JT 1236 HUN …
Nautica
  • 2,004
  • 1
  • 12
  • 35
1
2
3
41 42