将gsub应用于各种列 [英] Applying gsub to various columns
本文介绍了将gsub应用于各种列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将 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.
推荐答案
您可以使用应用
将其应用于整个数据。
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
这篇关于将gsub应用于各种列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文