Using ifelse to transform column in R

r ifelse %in%
if else in r data frame
ifelse vector r
mutate if else r
r inline if else statement
r replace values in dataframe conditional
how to use mutate with ifelse
r ifelse in list

I have a dataframe with a column of numbers.

In a separate column, I want to print whether the number is "less than 10", "between 10 and 20" or "between 20 and 30" based on the number.

I have produced this code so far which doesn't work so far, can anyone suggest how I can amend this so that it does?

#create some data

#ifelse statement
ifelse(data[,"number"]>=0&&<=9,"less than 10",
ifelse(data[,"number"]>=10&&<=20,"between 10 and 20",
ifelse(data[,"number"]>=20&&<=30,"between 20 and 30", "other")))  

The main problem was that you need to reference the variable in each inequality test. To make this more readable, I wrapped everything in a with(data... call. Another problem with your code was the use of && instead of &. The former is for single values only while the latter compares each element of two vectors.

       ifelse(number >= 0 & number <= 9, "less than 10",
       ifelse(number >= 10 & number <= 20, "between 10 and 20",
       ifelse(number >= 20 & number <= 30, "between 20 and 30", "other"))))

I also think this is a lot more readable than the tidyverse without introducing new syntax. It is easier to debug, too.

Creating New Variables in R with mutate() and ifelse(), vector with character strings, calling grepl() within the ifelse() clause adds to dplyr's functionality. grepl() is also from base R, and searches for  A third wheel: grepl () If you're working with a dataframe or vector with character strings, calling grepl () within the ifelse () clause adds to dplyr's functionality. grepl () is also from base R, and searches for matches to a character string specified in the first argument. If a match is found, grepl () returns TRUE.

You could use cut from base R, but be aware it makes the words variable a factor. You just need to set the appropriate intervals (which is why I used 30.5 etc for readibility). BTW, in your example you coded 20 should be recoded both to "between 10 and 20" and to "between 20 and 30", which won't work.

data$words <- cut(data$number, c(0,9.5,20.5,30.5,40), c("less than 10", "between 10 and 20", "between 20 and 30", "other"))

Manipulating data tables with dplyr, The basic set of R tools can accomplish many data table queries, but the syntax can be Tables can be subsetted by rows based on column values. Here, we make use of an embedded function, ifelse , which performs a One operation you cannot perform with recode is converting an NA level to another factor level. You could use cut from base R, but be aware it makes the words variable a factor. You just need to set the appropriate intervals (which is why I used 30.5 etc for readibility). BTW, in your example you coded 20 should be recoded both to "between 10 and 20" and to "between 20 and 30", which won't work.

 data %>% 
   mutate(word = case_when(
     number>=0 & number<10~"less than 10",
     number>=10 & number<20~"between 10 and 20",
     number>=20 & number<30~"between 20 and 30",
   number              word
1       1      less than 10
2       2      less than 10
3       3      less than 10
4       4      less than 10
5       5      less than 10
6       6      less than 10
7       7      less than 10
8       8      less than 10
9       9      less than 10
10     10 between 10 and 20
11     11 between 10 and 20
12     12 between 10 and 20
13     13 between 10 and 20
14     14 between 10 and 20
15     15 between 10 and 20
16     16 between 10 and 20
17     17 between 10 and 20
18     18 between 10 and 20
19     19 between 10 and 20
20     20 between 20 and 30
21     21 between 20 and 30
22     22 between 20 and 30
23     23 between 20 and 30
24     24 between 20 and 30
25     25 between 20 and 30
26     26 between 20 and 30
27     27 between 20 and 30
28     28 between 20 and 30
29     29 between 20 and 30
30     30             Other
31     31             Other
32     32             Other
33     33             Other
34     34             Other
35     35             Other
36     36             Other
37     37             Other
38     38             Other
39     39             Other
40     40             Other

ifelse: Conditional Element Selection, ifelse returns a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is TRUE​  There is an easier way to use if…else statement specifically for vectors in R programming. You can use ifelse() function instead; the vector equivalent form of the if…else statement. Check out these related examples:

do you need it to be all in one statement?

There are a few syntactical mistakes in your code, but a possible solution would be to do something like this

data$text <- "other"
data$text[data$number >=0 & data$number < 10] <- "less than 10"
data$text[data$number >=10 & data$number < 20] <- "between 10 and 20"
data$text[data$number >=20 & data$number < 30] <- "between 20 and 30"

I created a new column because if I were to replace the values in the 'number' column with text, the entire column would be coerced to character type and it might cause unexpected behaviour with the inequality operators.

You also have some overlap in your categories. Consider changing your upper bound to strictly less than (for example 20 is both >=20 and <=20, so falls into the "between 10 and 20" and "between 20 and 30" categories

If you want a one-liner, you can use the cut() function:

cut(data$number, breaks=c(0,10,20,30,Inf), 
labels=c("less than 10", "between 10 and 20", "between 20 and 30", "other"))

this turns a numeric vector into factor.

Basics of R Programming, What do you notice about how R creates a matrix from a vector? It constructs matrices set of row names. To convert htwtmatrix to a data frame, we use the command: ifelse(condition on data, true value returned, false returned). The above  Startup: Initialization at Start of an R Session stop: Stop Function Execution stopifnot: Ensure the Truth of R Expressions strptime: Date-time Conversion Functions to and from Character strrep: Repeat the Elements of a Character Vector strsplit: Split the Elements of a Character Vector strtoi: Convert Strings to Integers strtrim: Trim Character Strings to Specified Display Widths structure: Attribute Specification strwrap: Wrap Character Strings to Format Paragraphs subset: Subsetting

R ifelse() Function (With Example), This is a shorthand function to the traditional if…else statement. Vectors form the basic building block of R programming. Most of the functions in R take vector as  His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. David holds a doctorate in applied statistics. Tagged as: data cleaning , R , re-coding , Recoding

if ifelse() had more if's, I thought about using a nested ifelse statement, and I certainly could have done that. But that approach Let me translate the above into English. Then ( %>% )​, I use the mutate function to add a new column called group . baseR-V2016.2 - Data Management and Manipulation using R. Tested on R versions 3.0.X through 3.3.1 Last update: 15 August 2016

How to Use If-Else Statements and Loops in R – Dataquest, Learn to use if-else statements, for loops, and while loops to build complex we'​ll add an else statement to turn this into what's often called an if-else statement. Assuming that Team A's goals are listed first (the first index of the vector) and  R Replace NA with 0 (10 Examples for Data Frame, Vector & Column) A common way to treat missing values in R is to replace NA with 0. You will find a summary of the most popular approaches in the following.

  • ++ve for nice code and explanation, thanks for sharing.