Closed duttashi closed 5 years ago
Solution:
R> # create a dataframe
R> mydf<-data.frame(T=LETTERS[5:20],V=rnorm(16,10,1))
R> head(mydf)
T V
1 E 11.078
2 F 10.045
3 G 9.936
4 H 9.467
5 I 10.632
6 J 10.192
R> #1. Base R approach
R> mydf<-data.frame(T=LETTERS[5:20],V=rnorm(16,10,1))
R> levels(mydf$T)[levels(mydf$T) %in% c("E","S","T")] <- "AB"
R> head(mydf)
T V
1 AB 8.525
2 F 8.985
3 G 9.873
4 H 9.619
5 I 8.875
6 J 9.246
# 2. Non-Tidyverse approach
# use recode() from library(car)
library(car)
mydf<-data.frame(T=LETTERS[5:20],V=rnorm(16,10,1))
R> mydf$T<- recode(mydf$T,"c('E','S','T')='AB'")
R> head(mydf)
T V
1 AB 10.813
2 F 10.561
3 G 9.863
4 H 9.866
5 I 10.558
6 J 9.233
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Often in a data analysis project, there arises a need or requirement to replace values in either a single or multiple column. In this question, I will address this issue from two perspectives;
baseR
mutate
fromtidyverse
package