Gregory
Gregory

Reputation: 119

Replace random values in a column in a dataframe

Please advise how can I replace half of values in a column to NA:

# Generate 500 values with a skewed distribution
x1 <- round(rbeta(500,0.5,3)*100,0)

# Assign variable to a data frame
df <- data.frame(x1)

# Replace 250 random values in a column 'x1' to NA
df[sample(x1,250)] <- NA

The following mistake is shown:
Error in `[<-.data.frame`(`*tmp*`, sample(x1, 250), value = NA) : 
  new columns would leave holes after existing columns

I understand why the mistake is shown, but I would like to force the replacement. Please advise on how can I do that.

Upvotes: 9

Views: 2362

Answers (1)

G5W
G5W

Reputation: 37631

It seems like you need

df$x1[sample(nrow(df),250)] <- NA

Upvotes: 12

Related Questions