Applying gsub to various columns

What is the most efficient way to apply gsub to different columns? Does not work

 x1=c("10%","20%","30%") x2=c("60%","50%","40%") x3 = c(1,2,3) x = data.frame(x1,x2,x3) per_col = c(1,2) x = gsub("%","",x[,per_col]) 

How can I most effectively discard the "%" in the specified columns. Can I apply it to the entire data frame? This would be useful if I don't know where the percentage columns are.

+10
r dataframe gsub
source share
5 answers

You can use apply to apply it to the whole data.frame file

 apply(x, 2, function(y) as.numeric(gsub("%", "", y))) x1 x2 x3 [1,] 10 60 1 [2,] 20 50 2 [3,] 30 40 3 
+9
source share

Or you can try lapply solution:

 as.data.frame(lapply(x, function(y) gsub("%", "", y))) x1 x2 x3 1 10 60 1 2 20 50 2 3 30 40 3 
+7
source share

The first answer works, but be careful if you use data.frame with a string: @docendo discimus answer will return NAs .

If you want to save the contents of your column as a string, just delete as.numeric and convert your table into a data frame after:

 as.data.frame(apply(x, 2, function(y) as.numeric(gsub("%", "", y)))) x1 x2 x3 [1,] 10 60 1 [2,] 20 50 2 [3,] 30 40 3 
+2
source share

To add a docendo discimus answer , expanding with non-contiguous columns and returning data.frame :

 x1 <- c("10%", "20%", "30%") x2 <- c("60%", "50%", "40%") x3 <- c(1, 2, 3) x4 <- c("60%", "50%", "40%") x <- data.frame(x1, x2, x3, x4) x[, c(1:2, 4)] <- as.data.frame(apply(x[,c(1:2, 4)], 2, function(x) { as.numeric(gsub("%", "", x))} )) > x x1 x2 x3 x4 1 10 60 1 60 2 20 50 2 50 3 30 40 3 40 > class(x) [1] "data.frame" 
0
source share

To clear % you can do:

 x[per_col] <- lapply(x[per_col], function(y) as.numeric(gsub("%", "", y))) x x1 x2 x3 1 10 60 1 2 20 50 2 3 30 40 3 
0
source share

All Articles