Introduction
There are several ways to replace values in R. In this blog, you’ll learn how to replace everything from an individual value in a vector to all instances of a value across a data frame or in a vector.
Replace Values
Let’s create data we can experiment with:
test_data <- c(0, 0, 0, 0, 0, 0, 3, 3, 6, 8, 12, 13, 14, 15, 16, 19, 22, 50, 50, 100)
print(test_data)
Note that the 1 in brackets, [1], tells you that test_data is a vector, as there is only one row. When we start changing these data into data frames, you will note a change in the shape of the data from wide to long.
Now let’s say you want to replace the value in the first column, changing 0 to 1. Try:
test_data <- replace(test_data, 1, 1)
print(test_data)
Note that the replace function works on vectors, not data frames. What if the test_data vector was part of a data frame?
sub <- 1:20
df<-data.frame(sub, test_data)
print(df)
Now let’s say you want to turn the 1 in first column back to 0:
df$test_data <- replace(df$test_data, 1, 0)
print(df)
R has a command to change all values in a data frame. For example, if we wanted all 0 and 3 values to be 2, we could try:
df[df==0 | df == 3] <- 2
print(df)
Note the problem with this approach. The sub variable also had a value of 3 that was converted to 2. Replacing all values in a data frame is fine as long as you are sure that the changed values will be in the vector of your choice. So, if you wanted to change 0 and 3 to 2 in test_data but not sub, you would have to try another approach.
First, let’s recreate the data before we changed them:
test_data <- c(0, 0, 0, 0, 0, 0, 3, 3, 6, 8, 12, 13, 14, 15, 16, 19, 22, 50, 50, 100)
sub <- 1:20
df<-data.frame(sub, test_data)
print(df)
Now, to change all values of 0 and 3 in test_data but not in sub, try making test_data a data frame, using the replace command on it, and then putting it back into df.
test_data.df <-data.frame(df$test_data)
test_data.df[test_data.df ==0 | test_data.df == 3] <- 2
print(test_data.df)
Here, you can confirm that all the 0 and 3 values in test_data are now 2. Next:
testvector <- as.vector(test_data.df)
df_new<-data.frame(sub, testvector)
print(df_new)
There was some extra work involved in turning test_data into a data frame to change all values of 0 and 3 to 2, then into converting test_data back into a vector for inclusion in a new data frame.
You could have brute-forced your way through the problem by replacing values 1-8 of test_data with 2:
test_data.bf <- replace(test_data, 1:8, 2)
print(test_data.bf)
However, this kind of solution will only work if you have a few conveniently placed values to replace. In real-world data analysis, you will want to automate replacements using the approaches we showed you above.
BridgeText can help you with all of your statistical analysis needs.