Reputation: 557
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?
I tried the following:
mferg_1<-read.csv("file.csv",sep=";",dec=",",header=FALSE)
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
I thought that I could replace the commas by using gsub
or sub
:
mferg_1<-sub(",",".",mferg_1)
But then mferg_1
looks like this:
[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.
Upvotes: 3
Views: 16237
Reputation: 11597
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
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
Upvotes: 7