Replace given value in vector
Perhaps replace
is what you are looking for:
> x = c(3, 2, 1, 0, 4, 0)
> replace(x, x==0, 1)
[1] 3 2 1 1 4 1
Or, if you don't have x
(any specific reason why not?):
replace(c(3, 2, 1, 0, 4, 0), c(3, 2, 1, 0, 4, 0)==0, 1)
Many people are familiar with gsub
, so you can also try either of the following:
as.numeric(gsub(0, 1, x))
as.numeric(gsub(0, 1, c(3, 2, 1, 0, 4, 0)))
Update
After reading the comments, perhaps with
is an option:
with(data.frame(x = c(3, 2, 1, 0, 4, 0)), replace(x, x == 0, 1))
How to replace specific vector values?
Try this replacing scheme. The vector xori
has the original values whereas x
has been changed with different conditions. Additionally, the shortcut 1:3
is equivalent to c(1,2,3)
. Here the code:
#Code
set.seed(1)
x=sample(c(1,2,3,4,5,6,7,8,9),size=15,replace=TRUE)
xori <- x
#Replace
x[x %in% 1:3]<-1
x[x %in% 6:8]<-2
x[x %in% c(4,5,9)]<-3
#Print
xori
x
Output:
xori
[1] 9 4 7 1 2 7 2 3 1 5 5 6 7 9 5
x
[1] 3 3 2 1 1 2 1 1 1 3 3 2 2 3 3
If one way is required:
#One way
x <- ifelse(x %in% 1:3,1,
ifelse(x %in% 6:8,2,
ifelse(x %in% c(4,5,9),3,NA)))
Output:
x
[1] 3 3 2 1 1 2 1 1 1 3 3 2 2 3 3
Replace values from a column for vector value
x1<-merge(data.frame(A=x),data.frame(A=c('A','B','C'),B=c("Red,"Blue","Green")),by='A')[,2]
did that and do the trick
Is there a way to replace a character in a vector with a NULL value in R?
It's worth remembering the difference between NULL
and NA
. NA
values are a dodgy value, NULL
is no value whatsoever. In order to get the second output to be the same as the first output, you would have something the same as the following
column <- c("None", "Some", "NULL", "Many", "All")
column <- column[column != "NULL"]
This creates a shorter vector, which is why str_replace doesn't like it.
R - Given a List of Vectors replace values in Vectors for each Vector with LOOP
It seems to me that this is what you are looking for
vec1 <- list(c(1, 2, 3, 4, 5)) # as per your requirement
list_knot <- lapply(vec1[[1]], function(v, x) x - v^3, x = 1:800)
, i.e., subtract each element of vec1
from the same x
(1:800) and return a list of vectors. As you would like to replace every single element for each h
in list_knot
, you might just drop that list completely and construct a new one.
Search for and replace a value in a vector of structs
To change the elements in the vector, you need to take a reference in the range-for loop:
for (auto &e : data)
Otherwise you are making a copy, which doesn't change the original elements.
The 4th parameter of replace_if
needs to take an info
object, not just the new_name
value.
Instead of replace_if
, the appropriate algorithm to use here would be for_each
, which modifies the info
objects as needed:
std::for_each(data.begin(), data.end(),
[old_name, new_name](info &i) {
if (i.name == old_name)
i.name = new_name;
});
However, in this case, the range-for loop is probably less code, and easier to read and write.
Also, please avoid using namespace std;
, it's bad practice.
Replace values in a vector in R
If the outlier detection is based on values greater than 2000, then
data$students <- with(data, replace(students, students > 2000, mean(students)))
Regarding the mean
part, it is not clear whether the mean
takes the outlier values too. If it is not
i1 <- data$students >2000
data$students <- with(data, replace(students, i1, mean(students[i1])))
Related Topics
Dplyr Mutate with Conditional Values
When Importing CSV into R How to Generate Column with Name of the CSV
Creating a New Variable from a Lookup Table
The Cause of "Bad Magic Number" Error When Loading a Workspace and How to Avoid It
How to Take Pairwise Parallel Maximum Between Two Vectors
Check If Point Is in Spatial Object Which Consists of Multiple Polygons/Holes
Collapse Rows with Overlapping Ranges
How to Geocode a Simple Address Using Data Science Toolbox
How to Avoid Warning When Introducing Nas by Coercion
Creating a Prompt/Answer System to Input Data into R
Function to Calculate Geospatial Distance Between Two Points (Lat,Long) Using R
How to Install Development Version of R Packages Github Repository
How to Run R on a Server Without X11, and Avoid Broken Dependencies
Use Stat_Summary to Annotate Plot with Number of Observations
Label and Color Leaf Dendrogram
How to Draw a Line Across a Multiple-Figure Environment in R