Cannot convert alpha-numeric values in a dataset to just numeric

时间:2019-04-17 02:42:45

标签: r

I'm trying to get the m/z values and intensities of an experiment plotted as the x and y coordinates. I use the Cardinal MSI package to load in my data as nothing else can read my data files and average all intensities across a dataset for each m/z value. This works fine, but I then am left with a value that includes the format "m/z = 900.01" for the m/z value and just a number for the intensity.

So far, I've tried just using

data <- gsub("m/z = ", "", data)

which just eliminates the m/z value altogether. I've also tried converting to a dataframe instead of a value, this works fine but the m/z value is then a set of titles for the intensity. Again, trying the gsub removes the m/z value completely. Is there another way to remove the "m/z = " and get just the number that follows it, with that still connected to the intensity value? Thanks!

1 个答案:

答案 0 :(得分:0)

似乎您有一个命名向量,其中,名称"m/z = 899.91"的值为0.6186...,名称"m/z = 900.36"的值为{ {1}}等。向量是数字,并且所有值都是不到一的部分。

899.91、900.36等是名称的一部分。因此1.35811...应该让您获得名称的数字部分。

您可能要创建一个数据帧,其中一列用于m / z,一列用于强度:

as.numeric(gsub("m/z = ", "", names(data)))

然后您可以my_data = data.frame( mz = as.numeric(gsub("m/z = ", "", names(data))), intensity = data ) ,或进行其他更精美的绘图。