How to remove at the rate sign @ at last position from every value in R data frame column?

R ProgrammingServer Side ProgrammingProgramming

To remove at the rate sign @ at last position from every value in R data frame column, we can follow the below steps −

  • First of all, create a data frame with a column having at the rate sign @ at last position in every value.

  • Then, use gsub function to remove the at the rate sign @ at last position from every value in the column.

Example

Create the data frame

Let’s create a data frame as shown below −

Names<-
sample(c("emily@","sherjil@","nizam@","john@","michelle@","ronak@"),25,replace=TRUE)
df<-data.frame(Names)
df

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

    Names
1  michelle@
2  michelle@
3  sherjil@
4  nizam@
5  sherjil@
6  john@
7  michelle@
8  nizam@
9  nizam@
10 emily@
11 john@
12 john@
13 ronak@
14 michelle@
15 michelle@
16 ronak@
17 emily@
18 sherjil@
19 ronak@
20 sherjil@
21 michelle@
22 emily@
23 sherjil@
24 sherjil@
25 sherjil@

Remove at the rate (@) sign from last position

Using gsub function to remove the at the rate sign that is @ at last position from every value in column Names of data frame df as shown below −

Names<-
sample(c("emily@","sherjil@","nizam@","john@","michelle@","ronak@"),25,replace=TRUE)
df<-data.frame(Names)
df$new_Names<-gsub("@$","",df$Names)
df

Output

    Names    new_Names
1  michelle@ michelle
2  michelle@ michelle
3  sherjil@  sherjil
4  nizam@    nizam
5  sherjil@  sherjil
6  john@     john
7  michelle@ michelle
8  nizam@    nizam
9  nizam@    nizam
10 emily@    emily
11 john@     john
12 john@     john
13 ronak@    ronak
14 michelle@ michelle
15 michelle@ michelle
16 ronak@    ronak
17 emily@    emily
18 sherjil@  sherjil
19 ronak@    ronak
20 sherjil@  sherjil
21 michelle@ michelle
22 emily@    emily
23 sherjil@  sherjil
24 sherjil@  sherjil
25 sherjil@  sherjil
raja
Updated on 12-Nov-2021 07:37:28

Advertisements