How to replace a value in a data frame based on a conditional If statement

+1 vote

I tried replacing all of the times that B appears with b.

Consider the code below, to do the following:

junk <- data.frame(x <- rep(LETTERS[1:4], 3), y <- letters[1:12])
colnames(junk) <- c("alpha", "value")

this provides:

   alpha value
1   A    a
2   B    b
3   C    c
4   D    d
5   A    e
6   B    f
7   C    g
8   D    h
9   A    i
10  B    j
11  C    k
12  D    l

An initial attempt was to use a for and if statements like so:

for(i in junk$alpha) if(i %in% "B") junk$alpha <- "b"

but this replaces all of the values of junk$alpha with b.

 I know why this is doing this but I can't seem to get it to replace only those cases of junk$alpha where the original value was B.

Jun 6, 2018 in Data Analytics by DataKing99
• 8,250 points
36,633 views

2 answers to this question.

+1 vote

It's easier to convert alpha to characters and then make the change:

junk$alpha <- as.character(junk$alpha)

junk$alpha[junk$alpha == "B"] <- "b"

junk$alpha <- as.factor(junk$alpha)

Interested in a career in data analysis? Our Data Analyst Certification Course will equip you with the tools and techniques you need to succeed.

answered Jun 6, 2018 by Sahiti
• 6,370 points
0 votes

Hi,

You can see the below example. You will get the idea.

Let's create some data that we can use in the examples below.

data <- data.frame(num1 = 1:5,                # Example data
                   num2 = 3:7,
                   char = letters[1:5],
                   fac = c("gr1", "gr2", "gr1", "gr3", "gr2"))
data$char <- as.character(data$char)          # Convert to character
data                                          # Print example data
#   num1 num2 char fac
# 1    1    3    a gr1
# 2    2    4    b gr2
# 3    3    5    c gr1
# 4    4    6    d gr3
# 5    5    7    e gr2
data$num1[data$num1 == 1] <- 99               # Replace 1 by 99
data                                          # Print updated data
#   num1 num2 char fac
# 1   99    3    a gr1
# 2    2    4    b gr2
# 3    3    5    c gr1
# 4    4    6    d gr3
# 5    5    7    e gr2
answered Dec 15, 2020 by akhtar
• 38,260 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
2 answers

How to arrange a data set in ascending order based on a variable?

In your case it'll be, orderedviews = arrange(movie_views, ...READ MORE

answered Nov 27, 2018 in Data Analytics by Kalgi
• 52,350 points
1,083 views
+1 vote
3 answers

How to change the value of a variable using R programming in a data frame?

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$sym ...READ MORE

answered Jan 11, 2019 in Data Analytics by Tyrion anex
• 8,700 points
35,774 views
0 votes
1 answer

How to replace all occurrences of a character in a character column in a data frame in R

If you used sub() to replace the ...READ MORE

answered Jun 29, 2019 in Data Analytics by anonymous
• 33,030 points
17,915 views
0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
4,530 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
1,011 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
1,142 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,873 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,781 views
0 votes
2 answers

How to remove rows with missing values (NAs) in a data frame?

Hi, The below code returns rows without ...READ MORE

answered Aug 20, 2019 in Data Analytics by anonymous
• 33,030 points
14,735 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP