0 votes
1 view
in R Programming by (5.3k points)

In the R data frame coded for below, I would like to replace all of the times that B appears with b.

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

colnames(junk) <- c("nm", "val")

this provides:

   nm val

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

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

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

but as I am sure you can see, this replaces ALL of the values of junk$nm with b. I can see why this is doing this but I can't seem to get it to replace only those cases of junk$nm where the original value was B.

NOTE: I managed to solve the problem with gsub but in the interest of learning R I still would like to know how to get my original approach to work (if it is possible)

1 Answer

0 votes
by (25.3k points)

To replace values in a data frame based on a condition, you can use the following:

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

colnames(junk) <- c("nm", "val")

str(junk)

'data.frame': 12 obs. of  2 variables:

 $ nm : Factor w/ 4 levels "A","B","C","D": 1 2 3 4 1 2 3 4 1 2 ...

 $ val: Factor w/ 12 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ...

To convert factor column to a character for replacement:

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

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

Output:

 nm val

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

To convert the column back to a factor:

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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...