I'm trying to do a somewhat complicated task in R.
I have a data frame
with (for simplicity's sake) three columns.
Column 1 is a string.
Column 2 is an integer.
Column 3 is an integer.
I want to take all the observations which contain a certain substring in Column 1 AND that have an exact value for Column 2, and replace the third Column with the number 1.
That is, I have the following dataframe
:
x <- data.frame(x1 = c("bob","jane","bob","bobby","bob","jane","bobby","bob","jane","bob"),
x2 = c(1,1,1,1,1,2,2,2,2,2),
x3 = c(13,22,3,34,10,23,53,42,13,35))
And, I want to select observations where Column 1 contains bob and Column 2==1, and change the third column to 1, so that I end up with:
y1 <- c("bob","jane","bob","bobby","bob","jane","bobby","bob","jane","bob")
y2 <- c(1,1,1,1,1,2,2,2,2,2)
y3 <- c(1,22,1,1,1,23,53,42,13,35)
y <- data.frame(y1,y2,y3)
I want to do this across a really, really big dataset. It is not feasible to split up the dataset and put it back together.
I have tried using grep
, but it's not working when I try to do both matches at once. Also, I have tried subsetting, but then I'd have to split apart the dataframe
and put it back together.
Thanks very much in advance.