Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying gsub to various columns

Tags:

dataframe

r

gsub

What is the most efficient way to apply gsub to various columns? The following 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 efficiently drop the "%" sign in specified columns. Can I apply it to the whole dataframe? This would be useful in the case where I don't know where the percentage columns are.

like image 928
Richi W Avatar asked May 23 '14 14:05

Richi W


3 Answers

You can use apply to apply it to the whole 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
like image 80
talat Avatar answered Nov 09 '22 06:11

talat


Or, you could try the 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
like image 28
info_seekeR Avatar answered Nov 09 '22 06:11

info_seekeR


To clean the % out 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
like image 4
sindri_baldur Avatar answered Nov 09 '22 08:11

sindri_baldur