0
votes

Looking for some insight as to why I am getting the warning "number of items to replace is not a multiple of replacement length" with the simple code below (which does what I need it to do)?

A_DF <- data.frame(A = c(1,1,2,2,3,4), B = c(0,0,0,0,0,0))

A_DF <- A_DF %>% mutate(B = replace(B, A<=2, A), B = replace(B, A>2, 7))

Warning message:
In x[list] <- values :
  number of items to replace is not a multiple of replacement length

Output:

A_DF
  A B
1 1 1
2 1 1
3 2 2
4 2 2
5 3 7
6 4 7
1

1 Answers

0
votes

The problem is within

B = replace(B, A<=2, A)

A has a length of 6 to be squeeze into a subset of B ( when A<=2 ).

You can use:

A_DF <- A_DF %>% mutate(B = A, B = replace(B, A>2, 7))

Or:

A_DF$B <- ifelse(A_DF$A>2,7,A_DF$A)