从数据框中删除百分比 [英] Removing Percentages from a Data Frame

查看:94
本文介绍了从数据框中删除百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个源自Excel文件的数据帧。它在列之上有通常的标题,但有些列中有%符号,我想要删除。



搜索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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆