0
votes

I have a dataframe with an ID column and several columns of survey data. We now know that there was a glitch in the survey instrument, so entries are invalid for several consecutive columns for participants with IDs 3 through 11. We want to keep the data from these participants that IS valid, but change data in compromised columns from the current values to 99s. I can't share the survey data, so will explain what I'm hoping for using the iris dataset:

data("iris")

iris = 
  iris %>% 
  mutate(id = row_number())

The above code of course yields the following: enter image description here

So to solve my problem, I'm pretending entries with IDs 3 through 11 have compromised data for Sepal.Length, Sepal.Width, and Petal.Length, but that Petal.Width and Species are fine and should be left alone. How can I convert data for these columns to "99" in the specified rows, yielding the following?:

enter image description here

I know that I could do a long series of ifelse() statements, but that there's got to be a more straightforward approach. Any help is greatly appreciated!

1
iris[3:11, 1:3] <- 99tamtam

1 Answers

1
votes

Try this solution with across(). You can use ifelse() to test the id variable and the replace in the desired variables:

library(dplyr)
#Data
data("iris")
#Code
iris = 
  iris %>% 
  mutate(id = row_number()) %>%
  mutate(across(c(Sepal.Length:Petal.Length), ~ ifelse(id%in%3:11, 99, .)))

Output (some rows):

head(iris)

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species id
1          5.1         3.5          1.4         0.2  setosa  1
2          4.9         3.0          1.4         0.2  setosa  2
3         99.0        99.0         99.0         0.2  setosa  3
4         99.0        99.0         99.0         0.2  setosa  4
5         99.0        99.0         99.0         0.2  setosa  5
6         99.0        99.0         99.0         0.4  setosa  6