+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 35,695 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.

• 6,360 points

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```
• 38,240 points

## How to use a function to repeat a set of procedures on specific set of columns in a data frame?

You can parse the strings to symbols. ...READ MORE

+1 vote

## 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

## 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

## 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

## Big Data transformations with R

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

## Finding frequency of observations in R

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

+1 vote

## How to sort a data frame by columns in R?

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

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

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