How to remove a character in an R data frame column?

R ProgrammingServer Side ProgrammingProgramming

To remove a character in an R data frame column, we can use gsub function which will replace the character with blank. For example, if we have a data frame called df that contains a character column say x which has a character ID in each value then it can be removed by using the command gsub("ID","",as.character(df$x)).

Example1

Consider the below data frame −

Live Demo

> x1<-sample(c("Male1","Female1"),20,replace=TRUE)
> x2<-rpois(20,5)
> df1<-data.frame(x1,x2)
> df1

Output

        x1  x2
1    Male1  8
2  Female1  4
3    Male1  9
4    Male1  2
5    Male1  7
6  Female1  5
7    Male1  3
8  Female1  6
9  Female1  4
10 Female1  3
11   Male1  5
12   Male1  4
13 Female1  7
14   Male1  6
15 Female1  5
16   Male1  3
17 Female1 10
18 Female1  4
19   Male1  7
20 Female1  3

Removing 1 from all values in x1 −

> df1$x1<-gsub("1","",as.character(df1$x1))
> df1

Output

       x1  x2
1    Male  8
2  Female  4
3    Male  9
4    Male  2
5    Male  7
6  Female  5
7    Male  3
8  Female  6
9  Female  4
10 Female  3
11   Male  5
12   Male  4
13 Female  7
14   Male  6
15 Female  5
16   Male  3
17 Female 10
18 Female  4
19   Male  7
20 Female  3

Example2

Live Demo

> y1<-sample(c("Hot#","Cold#"),20,replace=TRUE)
> y2<-rnorm(20)
> df2<-data.frame(y1,y2)
> df2

Output

      y1         y2
1  Cold#  0.1725073
2   Hot# -0.4692023
3   Hot#  2.4386160
4  Cold# -1.3592685
5   Hot#  0.4500866
6  Cold# -0.7023989
7  Cold#  0.7383042
8   Hot# -1.7325754
9   Hot#  0.2053521
10  Hot# -2.0123750
11 Cold# -0.7203218
12  Hot#  1.2389079
13 Cold# -0.6401144
14 Cold# -0.6136728
15  Hot#  1.2877643
16 Cold#  0.4291966
17 Cold#  1.8316996
18  Hot#  0.8165865
19 Cold# -0.3978465
20 Cold# -0.9352063

Removing # from all values in y1 −

> df2$y1<-gsub("#","",as.character(df2$y1))
> df2

Output

     y1         y2
1  Cold  0.1725073
2   Hot -0.4692023
3   Hot  2.4386160
4  Cold -1.3592685
5   Hot  0.4500866
6  Cold -0.7023989
7  Cold  0.7383042
8   Hot -1.7325754
9   Hot  0.2053521
10  Hot -2.0123750
11 Cold -0.7203218
12  Hot  1.2389079
13 Cold -0.6401144
14 Cold -0.6136728
15  Hot  1.2877643
16 Cold  0.4291966
17 Cold  1.8316996
18  Hot  0.8165865
19 Cold -0.3978465
20 Cold -0.9352063
raja
Published on 06-Mar-2021 04:51:25
Advertisements