How to remove dot at last position from every value in R data frame column?



To remove dot 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 dot at last position in every value.

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

Example

Create the data frame

Let’s create a data frame as shown below −

x<-sample(c("A.","B.","C.","D.","E.","F.","G."),25,replace=TRUE) df<-data.frame(x) df

Output

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

 x 1 B. 2 D. 3 C. 4 F. 5 D. 6 E. 7 D. 8 C. 9 C. 10 A. 11 B. 12 E. 13 D. 14 D. 15 B. 16 D. 17 A. 18 F. 19 C. 20 G. 21 B. 22 B. 23 G. 24 G. 25 D.

Remove last dot

Using gsub function to remove the dot at last position from every value in column x as shown below −

x<-sample(c("A.","B.","C.","D.","E.","F.","G."),25,replace=TRUE) df<-data.frame(x) df$new_x<-gsub(".$","",df$x) df

Output

 x new_x 1 B. B 2 D. D 3 C. C 4 F. F 5 D. D 6 E. E 7 D. D 8 C. C 9 C. C 10 A. A 11 B. B 12 E. E 13 D. D 14 D. D 15 B. B 16 D. D 17 A. A 18 F. F 19 C. C 20 G. G 21 B. B 22 B. B 23 G. G 24 G. G 25 D. D
Updated on: 2021-11-09T06:28:15+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements