有什么办法在R中的矩阵中编辑值? [英] Any way to edit values in a matrix in R?

查看:96
本文介绍了有什么办法在R中的矩阵中编辑值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经解析了一个文件以提取某些值.列包含带符号的百分比.有什么办法可以删除%"字符?

I've parsed through a file to extract certain values. A column contains a percentage with the symbol. Is there any way to remove that "%" character?

从此:

98.9%   23    43
92.2%   342   34
98.9%   53    53
82.2%   32    76
97.9%   83    45
92.9%   92    23

收件人:

98.9   23    43
92.2   342   34
98.9   53    53
82.2   32    76
97.9   83    45
92.9   92    23

推荐答案

您在标题中说您有一个矩阵-在这种情况下,矩阵中的所有内容都应为字符".使用gsub将%替换为空.

You say in the title that you have a matrix - in which case everything in the matrix should be 'character' already. Use gsub to replace % with nothing.

> j <- matrix(c("1%", "2%", 3, 4), ncol = 2)
> j
     [,1] [,2]
[1,] "1%" "3" 
[2,] "2%" "4" 
> gsub("%", "", j)
     [,1] [,2]
[1,] "1"  "3" 
[2,] "2"  "4" 

如果您希望它是数字,则可以将applyas.numeric

if you want it to be numeric you could use apply along with as.numeric

> apply(gsub("%", "", j), 1, as.numeric)
     [,1] [,2]
[1,]    1    2
[2,]    3    4

这篇关于有什么办法在R中的矩阵中编辑值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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