- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to randomly replace values in an R data frame column?
The random replacement values in an R data frame column can be done with the help of sample function along with nrow function and single square subsetting.
For Example, if we have a data frame called df that contains a columns say X and we want to randomly replace 5 values in X with 1.5 then we can use the below given command −
df$X[sample(nrow(df),5)]<-1.5
Example 1
Following snippet creates a sample data frame −
x<-rnorm(20) df1<-data.frame(x) df1
The following dataframe is created
x 1 0.6329624 2 -0.1499087 3 1.4854089 4 -1.1517261 5 0.1337649 6 0.2561524 7 -0.5776149 8 -0.7255745 9 -0.5188515 10 -1.1959642 11 0.8444887 12 -0.4273016 13 -1.3380255 14 -0.3433049 15 -0.7926529 16 0.2302148 17 -0.3378159 18 -1.1405204 19 1.2611400 20 0.3000847
To randomly replace 10 values in x with 0 on the above created data frame, add the following code to the above snippet −
x<-rnorm(20) df1<-data.frame(x) df1$x[sample(nrow(df1),10)]<-0 df1
Output
If you execute all the above given snippets as a single program, it generates the following Output −
x 1 0.6329624 2 -0.1499087 3 0.0000000 4 -1.1517261 5 0.1337649 6 0.2561524 7 -0.5776149 8 0.0000000 9 -0.5188515 10 0.0000000 11 0.8444887 12 -0.4273016 13 0.0000000 14 0.0000000 15 0.0000000 16 0.0000000 17 0.0000000 18 0.0000000 19 1.2611400 20 0.0000000
Example 2
Following snippet creates a sample data frame −
y<-rpois(20,10) df2<-data.frame(y) df2
The following dataframe is created
y 1 9 2 7 3 8 4 5 5 9 6 14 7 11 8 15 9 15 10 11 11 13 12 12 13 14 14 11 15 15 16 11 17 12 18 10 19 12 20 11
To randomly replace 15 values in y with 1 on the above created data frame, add the following code to the above snippet −
y<-rpois(20,10) df2<-data.frame(y) df2$y[sample(nrow(df2),15)]<-1 df2
Output
If you execute all the above given snippets as a single program, it generates the following Output −
y 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 14 14 1 15 15 16 11 17 12 18 1 19 1 20 11
- Related Articles
- How to replace missing values with median in an R data frame column?
- How to replace missing values in a column with corresponding values in other column of an R data frame?
- How to replace a complete column in an R data frame?
- Replace numerical column values based on character column values in R data frame.
- How to replace NA with 0 and other values to 1 in an R data frame column?
- How to replace NA values with zeros in an R data frame?
- How to select positive values in an R data frame column?
- How to replace zero with previous value in an R data frame column?
- How to replace missing values with row means in an R data frame?
- How to replace NA values in columns of an R data frame form the mean of that column?
- How to create a repeated values column with each value in output selected randomly in R data frame?
- How to change row values based on column values in an R data frame?
- How to fill NA values with previous values in an R data frame column?
- How to replace $ sign combined with some specific values in an R data frame?
- How to replace numbers written in words with numbers in an R data frame column?
