用R中的科学符号用逗号替换逗号
我想读取一个使用科学计数法和逗号作为数据帧或向量中十进制分隔符的csv文件.然后,我想将这些数据用于进一步的计算.问题是R无法将数据识别为数字.如何将带逗号的科学计数法转换为带点的正确"科学计数法,以便用作数值?
I want to read a csv-file that uses scientific notation and comma as a decimal separator into a data frame or vector. Then I want to use this data for further calculations. The problem is that R does not recognize the data as numeric. How can I translate this scientific notation with commas into the "correct" scientific notation with dots so I can use as numeric values?
我尝试了以下操作:
mferg_1< -read.csv("file.csv",sep =;",dec =,",header = FALSE)
当我查看数据框 mferg_1
时,会出现以下内容(摘录):
When I look at the dataframe mferg_1
the following appears (excerpt):
V2 V3 V4 V5
14 3,063E+01 1,775E-02 6,641E-07 3,747E-02
我认为我可以使用 gsub
或 sub
替换逗号:
I thought that I could replace the commas by using gsub
or sub
:
mferg_1< -sub(,",.",mferg_1)
但是 mferg_1
看起来像这样:
[1] "425" "388" "535" "472"
我担心有一个简单的方法可以解决此问题,但是我找不到它.
I fear there is an easy way to solve this problem but I have not been able to find it.
您的csv(字符行)中可能有某些内容不允许将列转换为数字,因为 dec =,"
参数应该起作用.参见带有数据的以下示例:
You probably have something in your csv (a character line) that does not allow converting the columns to number, because the dec = ","
parameter should work. See this example with your data:
text <- "3,063E+01 1,775E-02 6,641E-07 3,747E-02"
read.table(text=text, dec = ",")
V1 V2 V3 V4
1 30.63 0.01775 6.641e-07 0.03747
现在,如果您不能确定问题(找到阻止R将您的列标识为数字的原因),则可以使用 gsub
.
Now, if you can't identify the problem (find what is preventing R to identify your columns as numeric), you could use gsub
.
df <- read.table(text=text)
df <- sapply(df, gsub, pattern = ",", replacement= ".")
df <- sapply(df, as.numeric)
V1 V2 V3 V4
3.063e+01 1.775e-02 6.641e-07 3.747e-02