These other responses were selected by only a subset of the Widget eaters (who are 10% of my respondents). This is a vectorised version of switch(): you can replace numeric values based on their position or their name, and character or factor values only by their name.This is an S3 generic: dplyr provides methods for numeric, character, and factors. Variables are always added horizontally in a data frame. Normally these are pretty easy to do, particularly when we are recoding off one variable, and that variable contains no missing values. Description. Details. This shows how I updated the 7th response option, which was coded to variable C1g in foo. R: Recoding variables using recode, mutate and case_when. Free 30 Day Trial

Specifically, if all of the variables are NA, ... recode variables with NAs with ifelse() in R. 0. r programming — conditional assignment producing unexpected results.

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under Today is a big day - recoding variables. Normally these are pretty easy to do, particularly when we are recoding off one variable, and that variable contains no missing values. This means that all respondents who did not have an “other” response for the question, regardless of whether or not they are Widget eaters, are NA for Other.Source in foo.First, updating an existing variable, based on values to two variables. Inside the master R data frame (“foo”), the 20 non-“Other” responses are coded as separate variables (because this is an “all that apply” question), from C1a to C1t, with C1r as the “Other” category. Private self-hosted questions and answers for your enterpriseProgramming and related technical career opportunities And this is where my ifelse has smacked up against the NA problem. I have had to use a slightly different approach to recode, depending on whether I am updating an existing variable, or creating a new one. This means that I have lots of information about the relevant 10% of the population, and a much smaller amount of information about the other 90% (age, location, and sex only).

I received back the recodes in the Excel file. The upshot is that I have a data frame where 90% of the data is NA.The current problem I am faced with is that there were a number of open-ended response options to questions because we had “Other, please specify” type options. your coworkers to find and share information. What does 'Argument 2 unnamed' mean?There are many to do this, probably many of them are easier than my approach.Thanks for contributing an answer to Stack Overflow! I am trying to replicate some code, but am running into trouble:However, when I execute code, I get the following error:I'm working in the latest version of R and using the Tidyverse package.What am I missing? variable C1a in foo), the value in Other.Source has been set to 1.I have 4 recodes to apply to existing response option categories, and 4 new response option categories that need to be constructed as new variables in the data frame (taking the range C1u to C1x to keep with the extant metadata framework). So the variable letters are one out.#code below is complicated because a missing value screws up the counts, so need to exclude missing values#explicitly, hence all the !is.na() syntax = ensuring that the value is NOT missing                      ifelse(foo$C1g==0 & !is.na(foo$Other.Source) & foo$Other.Source!=7,0,                             ifelse(foo$C1g==0 & is.na(foo$Other.Source),0,NA)))                      ifelse((is.na(foo$Other.Source) | foo$Other.Source!=25) & foo$UseMentioned==”Yes”,0,NA)) Once I figured out how to recode stuff, I found this to be completely false. The codes are:The Other.Source value equals the response option number for question C1 in the survey. Recode specifications appear in a character string, separated by semicolons (see the examples below), of the form input=output.If an input value satisfies more than one specification, then the first (from left to right) applies. These “Other” response options, which are answers to questions like “Where did you last buy Widgets”, “Where did you obtain information about the benefits of eating Widgets” were coded into updated categories by subject matter experts and provided to me as Excel files. Hopefully I can save someone else the time!My data frame is a survey of the general population, looking at consumption of a particular type of food (we’ll call it Widgets), because there was insufficient information about Widget-eaters to be able to cut a better sample frame.