How to Replace specific values in column in R DataFrame ?

It is safe to say that you are prepared to play a few games with your virtual deck? Hold on for a moment! The point framework in your deck of cards doesn’t adjust well to many games. For instance, in war and poker, aces are generally scored higher than lords. They’d have a point worth of 14, not 1.

In this undertaking, you will change the direct arrangement of your deck multiple times toward match three distinct games: war, hearts, and blackjack. Every one of these games will show you something else about altering the qualities within an informational collection. Start by making a duplicate of deck that you can control. This will guarantee that you generally have an immaculate duplicate of deck to count on (should things turn out badly):

In information investigation, there might be a lot of cases where you need to manage missing qualities, negative qualities, or non-exact qualities that are available in the dataset. These qualities may influence the examination result too. So to keep away from these circumstances and bogus correctnesses, you can utilize supplant() work in R to supplant the bogus qualities with proper qualities.

In this article, we will perceive how to supplant explicit qualities in a section of DataFrame in R Programming Language.

Strategy 1: Using Replace() work.

supplant() work in R Language is utilized to supplant the qualities in the predefined string vector x with lists given in list by those given in qualities.

Language structure: replace(list , position , replacement_value)

It takes on three boundaries initially is the rundown name, then, at that point, the record at which the component should be supplanted, and the third boundary is the substitution esteems.

Example 1: Replace Particular Value Across Entire Data Frame

The following code shows how to replace one particular value with a new value across an entire data frame:

#create data frame
df <- data.frame(a = as.factor(c(1, 5, 7, 8)),
                 b = c('A', 'B', 'C', 'D'),
                 c = c(14, 14, 19, 22),
                 d = c(3, 7, 14, 11))

#view data frame
df

  a b  c  d
1 1 A 14  3
2 5 B 14  7
3 7 C 19 14
4 8 D 22 11

#replace '14' with '24' across entire data frame
df[df == 14] <- 24

#view updated data frame
df 

  a b  c  d
1 1 A 24  3
2 5 B 24  7
3 7 C 19 24
4 8 D 22 11

Example 2: Replace One of Several Values Across Entire Data Frame

The following code shows how to replace one of several values with a new value across an entire data frame:

#create data frame
df <- data.frame(a = as.factor(c(1, 5, 7, 8)),
                 b = c('A', 'B', 'C', 'D'),
                 c = c(14, 14, 19, 22),
                 d = c(3, 7, 14, 11))

#view data frame
df

  a b  c  d
1 1 A 14  3
2 5 B 14  7
3 7 C 19 14
4 8 D 22 11

#replace '14' and '19' with '24' across entire data frame
df[df == 14 | df == 19] <- 24

#view updated data frame
df

  a b  c  d
1 1 A 24  3
2 5 B 24  7
3 7 C 24 24
4 8 D 22 11

Example 3: Replace Value in Specific Column of Data Frame

The following code shows how to replace one particular value with a new value in a specific column of a data frame:

#create data frame
df <- data.frame(a = as.factor(c(1, 5, 7, 8)),
                 b = c('A', 'B', 'C', 'D'),
                 c = c(14, 14, 19, 22),
                 d = c(3, 7, 14, 11))

#view data frame
df

  a b  c  d
1 1 A 14  3
2 5 B 14  7
3 7 C 19 14
4 8 D 22 11

#replace '14' in column c with '24'
df['c'][df['c'] == 14] <- 24

#view updated data frame
df 

  a b  c  d
1 1 A 24  3
2 5 B 24  7
3 7 C 19 14
4 8 D 22 11

Example 4: Replace Values of a Factor Variable in Data Frame

If you attempt to replace a particular value of a factor variable, you will encounter the following warning message:

#create data frame
df <- data.frame(a = as.factor(c(1, 5, 7, 8)),
                 b = c('A', 'B', 'C', 'D'),
                 c = c(14, 14, 19, 22),
                 d = c(3, 7, 14, 11))

#attempt to replace '1' with '24' in column a
df['a'][df['a'] == 1] <- 24

Warning message:
In `[<-.factor`(`*tmp*`, thisvar, value = 24) :
  invalid factor level, NA generated
     a b  c  d
1 <NA> A 14  3
2    5 B 14  7
3    7 C 19 14
4    8 D 22 11

To avoid this warning, you need to first convert the factor variable to a numeric variable:

#convert column a to numeric
df$a <- as.numeric(as.character(df$a))

#replace '1' with '24' in column a
df['a'][df['a'] == 1] <- 24

#view updated data frame
df
   a b  c  d
1 24 A 14  3
2  5 B 14  7
3  7 C 19 14
4  8 D 22 11

Also Read: How to import a Python module given the full path?

Leave a Reply

Your email address will not be published. Required fields are marked *