So I have a data frame that looks like
df |
---|
1 |
1 |
1 |
1 |
1 |
1 |
0 |
0 |
0 |
Is there a way to change just the first zero, the one in row 7, to a different value I have stored in my environment. In this exact case it's the value is stored under
new <- .4
The first 0 is in a different place for each dataset I have, some are dozens of rows long.
CodePudding user response:
There should be multiple ways to do this.
Here is an option with match
-
data <- data.frame(num = rep(c(1, 0), c(6, 3)))
new <- .4
data$num[match(0, data$num)] <- new
data
# num
#1 1.0
#2 1.0
#3 1.0
#4 1.0
#5 1.0
#6 1.0
#7 0.4
#8 0.0
#9 0.0
match
would return index of 1st matching value.
CodePudding user response:
For replacing a single value at a specific location you can use
df["Row number", "Column number"] = "New value"
In your case that would be:
df[7,1] = 0.4
or for the stored variable: df[7,1] = new