从数据框中删除百分比 [英] Removing Percentages from a Data Frame
问题描述
搜索stackoverflow提供了一些很好的代码,用于从矩阵中删除百分比,任何方式在R中编辑矩阵中的值? ,当我试图将它应用到我的数据框时,它不起作用
as.numeric(gsub(% ,,my.dataframe))
而是只返回一个NA字符串有一个警告信息,说明它们是通过强制引入的。当我申请
gsub(%,,my.dataframe))
我得到了c(...)形式的值,其中...代表后缀为逗号的数字,我的每一列。没有%被证明;如果我可以把它放在一起...我会做饭。
任何帮助都非常受欢迎,谢谢。
基于@ Arun的评论和成像数据框架如何:
> DF< - data.frame(X = paste0(1:5,'%'),
Y = paste0(2 *(1:5),'%'),
Z = 3 * (1:5),stringsAsFactors = FALSE)
> DF#这是我如何想象你的数据框架看起来像
XYZ
1 1%2%3
2 2%4%6
3 3%6%9
4 4%8%12
5 5%10%15
> #使用@ Arun的建议
> (DF2< - data.frame(sapply(DF,function(x)as.numeric(gsub(%,,x)))))
XYZ
1 1 2 3
2 2 4 6
3 3 6 9
4 4 8 12
5 5 10 15
我在 sapply
中添加了 as.numeric
,调用结果列为数字,如果我不使用 as.numeric
,结果将是因素
。使用 sapply(DF2,class)
I have a dataframe that originated from an excel file. It has the usual headers above the columns but some of the columns have % signs in them which I want to remove.
Searching stackoverflow gives some nice code for removing percentages from matrices, Any way to edit values in a matrix in R?, which did not work when I tried to apply it to my dataframe
as.numeric(gsub("%", "", my.dataframe))
instead it just returns a string of "NA"s with a warning message explaining that they were introduced by coercion. When I applied,
gsub("%", "", my.dataframe))
I got the values in "c(...)" form, where the ... represent numbers followed by commas which was reproduced for every column that I had. No % was in evidence; if I could just put this back together ... I'd be cooking.
Any help greatfully received, thanks.
Based on @Arun's comment and imaging how your data.frame looks like:
> DF <- data.frame(X = paste0(1:5,'%'),
Y = paste0(2*(1:5),'%'),
Z = 3*(1:5), stringsAsFactors=FALSE )
> DF # this is how I imagine your data.frame looks like
X Y Z
1 1% 2% 3
2 2% 4% 6
3 3% 6% 9
4 4% 8% 12
5 5% 10% 15
> # Using @Arun's suggestion
> (DF2 <- data.frame(sapply(DF, function(x) as.numeric(gsub("%", "", x)))))
X Y Z
1 1 2 3
2 2 4 6
3 3 6 9
4 4 8 12
5 5 10 15
I added as.numeric
in sapply
call for the resulting cols to be numeric, if I don't use as.numeric
the result will be factor
. Check it out using sapply(DF2, class)
这篇关于从数据框中删除百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!