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

### Related Posts:

- How do I select the first row in an R data frame that meets certain criteria?
- Pig Latin Translator
- Poker hand range chart visualization in R
- Critical t values in R
- Error in
: object of type ‘closure’ is not subsettable - What does %>% function mean in R?
- How to join (merge) data frames (inner, outer, left, right)
- How to coerce a list object to type ‘double’
- R Error in x$ed : $ operator is invalid for atomic vectors
- Is double square brackets [[ ]] preferable over single square brackets [ ] in Bash?
- What does na.rm=TRUE actually means?
- Remove legend ggplot 2.2
- How do I replace NA values with zeros in an R dataframe?
- R on MacOS Error: vector memory exhausted (limit reached?)
- Python inline if statement
- Why use as.factor() instead of just factor()
- Update R using RStudio
- mean() warning: argument is not numeric or logical: returning NA
- ‘x’ and ‘y’ lengths differ ERROR when plotting
- Why do I get “number of items to replace is not a multiple of replacement length”
- Drop data frame columns by name
- How can two strings be concatenated?
- Remove a row from a data table in R
- “Error: Continuous value supplied to discrete scale” in default data set example mtcars and ggplot2
- Why am I getting “algorithm did not converge” and “fitted prob numerically 0 or 1” warnings with glm?
- How does one reorder columns in a data frame?
- Arcsine transformation of percentage data
- R loop that adds following numbers to each element in list
- Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) 0 non-na cases
- Can I calculate z-score with R?
- How do I perform an IF…THEN in an SQL SELECT?
- Using Caret Package but Getting Error in library(e1071)
- Function to clear the console in R and RStudio
- plot.new has not been called yet
- R: need finite ‘ylim’ values in function
- How to save a data frame as CSV to a user selected location using tcltk
- Convert a list to a data frame
- Why I get this error writing data to a file
- Remove NA values from a vector
- character string is not in a standard unambiguous format
- Remove rows with all or some NAs (missing values) in data.frame
- duplicate ‘row.names’ are not allowed error
- How to count the number of observations in R like Stata command count
- Error: unexpected ‘}’ in “}” in R [duplicate]
- Error : length of ‘dimnames’ [2] not equal to array extent
- Mean of a column in a data frame, given the column’s name
- What is the difference between rm() and rm(list=ls())?
- Subscript out of bounds – general definition and solution?
- Understanding `scale` in R
- Error with apply function
- file.path function in R
- Reshaping data.frame from wide to long format
- Remove rows with all or some NAs (missing values) in data.frame
- Converting data frame column from character to numeric
- How to set limits for axes in ggplot2 R plots?
- Rotating and spacing axis labels in ggplot2
- duplicate ‘row.names’ are not allowed error
- How to write an inline IF statement in JavaScript?
- How to sum data.frame column values?
- Error: invalid subscript type ‘list’ in R
- Is there a dictionary functionality in R
- “non-numeric argument to binary operator” error in R
- ggplot2 manually specifying colour with geom_line
- How to update a package in R?
- Error: Invalid input: date_trans works with objects of class Date only
- Matrix expression causes error “requires numeric/complex matrix/vector arguments”?
- NAs are not allowed in subscripted assignments
- Non-conformable arrays error in code
- Unused arguments in R
- How to convert a data frame column to numeric type?
- Boxplot dplyr: Error: non-numeric argument to binary operator
- Error: ggplot2 doesn’t know how to deal with data of class matrix?
- lmer error: grouping factor must be < number of observations
- Aggregate multiple columns at once
- installing data.table results in “there is no package”
- What is integer overflow in R and how can it happen?
- Creating box plot on exercise
- changing title in multiplot ggplot2 using grid.arrange
- How to split data into training/testing sets using sample function
- How to sort a data frame by alphabetic order of a character variable in R?
- Error in na.fail.default(as.ts(x)) : missing values in object in time series forecasting
- ORA-06502: PL/SQL: numeric or value error: character string buffer too small
- Error in contrib.url(repos, “source”) in R trying to use CRAN without setting a mirror Calls: install.packages -> contrib.url Execution halted
- Geometric Mean: is there a built-in?
- Error in xj[i] : invalid subscript type ‘list’
- Error in eval(predvars, data, env) : invalid ‘envir’ argument of type ‘character’
- How to resolve “Syntax error on token “else”” in Java?
- if else function in pandas dataframe
- Convert the values in a column into row names in an existing data frame
- Does Lua have OR comparisons?
- How can I make a list of lists in R?
- Merge data frames based on rownames in R
- The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
- If variable equals value php [duplicate]
- Subset and ggplot2
- How to resolve “Syntax error on token “else”” in Java?
- Styling multi-line conditions in ‘if’ statements?
- How do I print a hexadecimal number with leading 0 to have width 2 using sprintf?
- Compare two columns using pandas
- How to deal with “data of class uneval” error from ggplot2?