maybe you want ifelse
:
a <- c(1,1,1,1,0,0,0,0,2,2)
ifelse(a>0,a/sum(a),1)
[1] 0.125 0.125 0.125 0.125 1.000 1.000 1.000 1.000
[9] 0.250 0.250
More Related Contents:
- How to prevent ifelse() from turning Date objects into numeric objects
- Does ifelse really calculate both of its vectors every time? Is it slow?
- Nested ifelse statement
- Why can’t R’s ifelse statements return vectors?
- Vectorized IF statement in R?
- Can dplyr package be used for conditional mutating?
- The condition has length > 1 and only the first element will be used
- else if(){} VS ifelse()
- Replace negative values by zero
- if/else constructs inside and outside functions
- if-else vs ifelse with lists
- if – else if – else statement and brackets
- Cumulative sum until maximum reached, then repeat from zero in the next row
- R ifelse statement
- Unexpected ‘else’ in “else” error
- Create column with grouped values based on another column
- dplyr if_else() vs base R ifelse()
- Using ifelse in R
- Difference between if() and ifelse() functions
- if else condition in ggplot to add an extra layer
- Create new column based on 4 values in another column
- Assign a value, if a number is in between two numbers
- Adding new column with conditional values using ifelse
- How do I create a new column based on multiple conditions from multiple columns?
- Return a matrix with `ifelse`
- Keeping trailing zeros
- How to change order of boxplots when using ggplot2?
- listing contents of an R data file without loading
- R: Plotting a 3D surface from x, y, z
- For the same code, labels (q1, median) appear on one computer but don’t appear on another computer