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
4
votes
2 answers

R - Recoding column with multiple text values associated with one code

I'm trying to recode a column to determine the shift of an employee. The data is messy and the word I am looking for must be extracted from the text. I've been trying various routes with if statements, stringr and dplyr packages, but can't figure…
britt
  • 79
  • 10
4
votes
1 answer

Turning a continuous variable into categorical in Spark

I am trying to turn some continuous variables into categorical variables in order to apply some ML algorithms to them and I want to make categories like from 6:00 to 12:00 --> "Morning" or dates in a format like ddMM to "Summer" or whatever. These…
Borja_042
  • 1,071
  • 1
  • 14
  • 26
4
votes
3 answers

recode (car package) function - recodes argument and equals sign

I would like know whether it is permitted to use an equals (=) sign in the recodes parameter of the recode function in the car package? For instance, the following fails: library(car) n <- c(0, 10, 20, 21, 60, 70) r <- recode(n, " 0:20 = '<= 20' ;…
user6155802
4
votes
3 answers

Setting *most* variables to missing, while preserving the contents of a select few

I have a dataset like this (but with several hundred vars): id q1 g7 q3 b2 zz gl az tre 1 1 2 1 1 1 2 1 1 2 2 3 3 2 2 2 1 1 3 1 2 3 3 2 1 3 3 4 3 1 2 2 3 2 1 1 5 2 1 2 2…
J.Q
  • 971
  • 1
  • 14
  • 29
4
votes
1 answer

Recode values omitting NA's

I want to recode the values in a matrix in such a way that all values <=.2 become 2, <=.4 become 3 etc. However, there are missings in my data, which I do not want to change (keep them NA). Here you find a simplified version of my code. Using…
VandenEnden
  • 125
  • 1
  • 1
  • 5
4
votes
2 answers

Recoding groups of individuals

I am trying to renumber groups of people. In the data, 'FamID' indicates a family, 'PtID' indicates an individual patient that relates to the family. The 'Twin' column indicates whether the patients are identical twins (coded as 1), non-identical…
Sheila
  • 2,438
  • 7
  • 28
  • 37
3
votes
2 answers

Recoding multiple factors using regexp

I have data from a survey, where several questions are in the format "Do you think that [xxxxxxx]" The possible answers to the questions are in the format "I am certain that [xxxxxxx]" "I think it is possible that [xxxxxx]" "I don't know if…
nico
  • 50,859
  • 17
  • 87
  • 112
3
votes
4 answers

How to count the number of occurrences of a given value for each row?

I'm sure this is a really easy fix but I can't seem to find the answer... I am trying to create a column at the end of my dataframe that is a sum of the number of times a specific value (say "1") appears across that row. So for example, if I started…
user21027866
  • 129
  • 5
3
votes
3 answers

Copying information over when there are multiple datapoints

I have a data cleaning question. The data collection happened three times and sometimes the data entry was incorrect. Therefore, if the students had their data collected more than one time, the second data point needs to be copied over. Here is my…
amisos55
  • 1,913
  • 1
  • 10
  • 21
3
votes
0 answers

Replace() function with multiple values simultaneously recoded in r

This is a very basic question, but I am using the replace() function to recode values that switch half-way through the years of reporting in my dataset. The key switches, so I am converting old character values to new ones. Example: For the variable…
flâneur
  • 633
  • 2
  • 8
3
votes
2 answers

create new order for existing column values without reordering rows in dataframe - R

I have some results cluster labels from kmeans done on different ids (reprex example below). the problem is the kmeans clusters codes are not ordered consistently across ids although all ids have 3 clusters. reprex = data.frame(id = rep(1:2, each =…
Myriad
  • 341
  • 1
  • 8
3
votes
2 answers

Recoding values in second data frame based on values in a different data frame

I want to recode the values in my second data frame based on the corresponding value in a different data frame. for example, here's what the first data frame looks like. Each code is assigned to a corresponding…
3
votes
4 answers

Performing Case Specific Edits in For Loop in R

I have survey data that require several case specific recodes that I need to perform, but I want to avoid creating a new line of code for each and every recode (because there will be dozens). I am hoping there is a way I can recode the data using a…
Quinterpret
  • 133
  • 6
3
votes
4 answers

How pivot longer and match the variable name with a secondary table?

I'm struggling with the logic in order to cross these 2 tables: My dataframes are: df6 includes an id and a numeric value per column. v1 v2 and v3 will always have one and only one non-zero value, which indicates the yummyness value of that…
Andres Mora
  • 1,040
  • 8
  • 16
3
votes
2 answers

Selecting a large range of values to recode in R

I want to recode a large number of variables across multiple columns. Here is an example df df <- data.frame( id_number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), age = c(10, 11, 12, 13, 14, 15, 16, 17, 18, 19), abc1 = c(501, 502, 503, 504, 505, 506,…
rais
  • 81
  • 6
1 2
3
41 42