Home > Mobile >  R error "number of items to replace is not a multiple of replacement length"
R error "number of items to replace is not a multiple of replacement length"

Time:05-10

I'm new to R and full disclosure this is a homework assignment. Although the problem is outside of the scope of my assignment. I have some random medical data with a lot of NA's and outliers. I wanted to select all the values of 8 or higher in the Children column that corresponds to people under the age of 60 and replace them with an NA value. I used this code and it does work even if it's not pretty:

x = md$Children 
y = md$Age 
md$Children[which(x >= 8 & y < 60)] = is.na(x)

But when I run it I get the

Warning message: In md$Children[which(x >= 8 & y < 60)] = is.na(x) : number of items to replace is not a multiple of replacement length.

I know I have fewer replacements than there are rows in my data but is there a way to do this better or does the warning just not matter?

CodePudding user response:

md$Children[which(x >= 8 & y < 60)] = NA

CodePudding user response:

@AbdurRohman's answer is good.

You could use

md <- within(md,
   Children[Children >= 8 & Age <60] <- NA
)

for slightly clearer code. (You should definitely tell your instructor you got help on Stack Overflow.)

  •  Tags:  
  • r
  • Related