I am trying to replace two values, Value
and Value1
, in a row with the specific name (Description
).
df <- data.frame(
Description = c("15A","11","12"),
Value = c(200,196,NA),
Value1 = c(300,196,NA),
ValueTested = c(20,10,NA),
ApplyValues = c(60,10,NA)
)
df
Now I expect from the code, first to find the row in column Description with value 15A
and to change the corresponding row in the column Value
( from 200 to 100 ) and Value1
(from 300 to 150). I tried with the code below, but this code is related to the position of value 15A
. So if this value goes, let's say, down this line will not work.
df$Value[1] = 100
df$Value1[1]= 100
So can anybody help me how to solve this problem and get the result as a result below
CodePudding user response:
With dplyr
, you can use rows_update()
to modify rows of a table according to another table of data by matching a set of key variables.
library(dplyr)
df %>%
rows_update(tibble(Description = '15A', Value = 100, Value1 = 150),
by = "Description")
# Description Value Value1 ValueTested ApplyValues
# 1 15A 100* 150* 20 60
# 2 11 196 196 10 10
# 3 12 NA NA NA NA